Lock.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
13 /**
14  * Lock represents a lock on an object.
15  *
16  * @author ingo herwig <ingo@wemove.com>
17  */
18 class Lock implements \Serializable {
19 
20  const TYPE_OPTIMISTIC = 'optimistic';
21  const TYPE_PESSIMISTIC = 'pessimistic'; // pessimistic write lock
22 
23  private $type = null;
24  private $objectId = null;
25  private $login = "";
26  private $created = "";
27  private $currentState = null;
28 
29  /**
30  * Creates a lock on a given object.
31  * @param $type One of the Lock::Type constants
32  * @param $oid ObjectId of the object to lock
33  * @param $login Login name of the user who holds the lock
34  * @param $created Creation date of the lock. If omitted the current date will be taken.
35  */
36  public function __construct($type, $oid, $login, $created='') {
37  $this->type = $type;
38  $this->objectId = $oid;
39  $this->login = $login;
40  if ($created == '') {
41  $this->created = date("Y-m-d H:i:s");
42  }
43  else {
44  $this->created = $created;
45  }
46  }
47 
48  /**
49  * Get the type of the lock.
50  * @return One of the Lock::Type constants.
51  */
52  public function getType() {
53  return $this->type;
54  }
55 
56  /**
57  * Get the oid of the locked object.
58  * @return ObjectId of the locked object.
59  */
60  public function getObjectId() {
61  return $this->objectId;
62  }
63 
64  /**
65  * Get the login of the user who holds the lock.
66  * @return The login of the user.
67  */
68  public function getLogin() {
69  return $this->login;
70  }
71 
72  /**
73  * Get the creation date/time of the lock.
74  * @return The creation date/time of the lock.
75  */
76  public function getCreated() {
77  return $this->created;
78  }
79 
80  /**
81  * Set the original state of the object in case of an
82  * optimistic lock.
83  * @param $currentState PersistentObject instance or null
84  */
85  public function setCurrentState($currentState) {
86  $this->currentState = serialize($currentState);
87  }
88 
89  /**
90  * Get the original state of the object in case of an
91  * optimistic lock.
92  * @return PersistentObject instance or null
93  */
94  public function getCurrentState() {
95  return unserialize($this->currentState);
96  }
97 
98  public function serialize() {
99  return serialize([$this->type, serialize($this->objectId),
100  $this->login, $this->created, serialize($this->currentState)]);
101  }
102 
103  public function unserialize($data) {
104  $parts = unserialize($data);
105  $this->type = $parts[0];
106  $this->objectId = unserialize($parts[1]);
107  $this->login = $parts[2];
108  $this->created = $parts[3];
109  $this->currentState = unserialize($parts[4]);
110  }
111 }
112 ?>
getCreated()
Get the creation date/time of the lock.
Definition: Lock.php:76
getLogin()
Get the login of the user who holds the lock.
Definition: Lock.php:68
getObjectId()
Get the oid of the locked object.
Definition: Lock.php:60
Lock represents a lock on an object.
Definition: Lock.php:18
__construct($type, $oid, $login, $created='')
Creates a lock on a given object.
Definition: Lock.php:36
setCurrentState($currentState)
Set the original state of the object in case of an optimistic lock.
Definition: Lock.php:85
getCurrentState()
Get the original state of the object in case of an optimistic lock.
Definition: Lock.php:94
getType()
Get the type of the lock.
Definition: Lock.php:52