1 | <?php |
||
61 | class Manager implements ManagerInterface |
||
62 | { |
||
63 | /** |
||
64 | * Create and return a new object |
||
65 | * |
||
66 | * @param RepositoryInterface $repository Repository to create the object in |
||
67 | * @param Type $type Object type |
||
68 | * @param string $payload Object payload |
||
69 | * @param array $propertyData Object property data |
||
70 | * @param \DateTimeInterface $creationDate Object creation date |
||
71 | * @return ObjectInterface Object |
||
72 | */ |
||
73 | 7 | public function createObject( |
|
74 | RepositoryInterface $repository, |
||
75 | Type $type, |
||
76 | $payload = '', |
||
77 | array $propertyData = [], |
||
78 | \DateTimeInterface $creationDate = null |
||
79 | ) { |
||
80 | // Set the creation date to now if empty |
||
81 | 7 | if ($creationDate === null) { |
|
82 | 5 | $creationDate = new \DateTimeImmutable('now'); |
|
83 | } |
||
84 | |||
85 | // Construct a creation closure |
||
86 | 7 | $creationClosure = function (Id $uid) use ($repository, $type, $payload, $propertyData, $creationDate) { |
|
87 | /** @var Revision $revision */ |
||
88 | 6 | $revision = Kernel::create(Revision::class, [1, true]); |
|
89 | |||
90 | /** @var RepositoryPath $repositoryPath */ |
||
91 | 6 | $repositoryPath = Kernel::create(RepositoryPath::class, [$repository]); |
|
92 | 6 | $repositoryPath = $repositoryPath->setId($uid); |
|
93 | 6 | $repositoryPath = $repositoryPath->setRevision($revision); |
|
94 | 6 | $repositoryPath = $repositoryPath->setType($type); |
|
95 | 6 | $repositoryPath = $repositoryPath->setCreationDate($creationDate); |
|
96 | |||
97 | 6 | return ObjectFactory::createFromParams($repositoryPath, $payload, $propertyData); |
|
98 | 7 | }; |
|
99 | |||
100 | // Wrap the object creation in an ID allocation transaction |
||
101 | 7 | return $repository->getAdapterStrategy()->createObjectResource($creationClosure); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Load an object from a repository |
||
106 | * |
||
107 | * @param RepositoryPathInterface $path Repository object path |
||
108 | * @param int $visibility Object visibility |
||
109 | * @return ObjectInterface Object |
||
110 | */ |
||
111 | 33 | public function loadObject(RepositoryPathInterface $path, $visibility = SelectorInterface::ALL) |
|
112 | { |
||
113 | // Create the current revision path |
||
114 | /** @var RepositoryPathInterface $currentPath */ |
||
115 | 33 | $currentPath = $path->setRevision(Revision::current()); |
|
116 | |||
117 | // Load the object resource respecting visibility constraints |
||
118 | 33 | $objectResource = $this->loadObjectResource($currentPath, $visibility); |
|
119 | |||
120 | // Instantiate the object |
||
121 | 31 | $object = ObjectFactory::createFromResource($currentPath, $objectResource); |
|
122 | |||
123 | // Use and return the requested object revision |
||
124 | 31 | return $object->useRevision($path->getRevision()); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Load and return an object resource respecting visibility constraints |
||
129 | * |
||
130 | * @param RepositoryPathInterface $currentPath |
||
131 | * @param int $visibility Object visibility |
||
132 | * @return ResourceInterface Object resource |
||
133 | * @throws InvalidArgumentException If the resource could not be loaded |
||
134 | */ |
||
135 | 34 | public function loadObjectResource(RepositoryPathInterface &$currentPath, $visibility = SelectorInterface::ALL) |
|
177 | |||
178 | /** |
||
179 | * Validate a given object visibility |
||
180 | * |
||
181 | * @param int $visibility Object visibility |
||
182 | * @throw RepositoryInvalidArgumentException If the visibility requirement is invalid |
||
183 | */ |
||
184 | 34 | protected function validateVisibility($visibility) |
|
199 | |||
200 | /** |
||
201 | * Instantiate object resource |
||
202 | * |
||
203 | * @param RepositoryPathInterface $path |
||
204 | * @return ResourceInterface Object resource |
||
205 | */ |
||
206 | 33 | public function getObjectResource(RepositoryPathInterface $path) |
|
212 | |||
213 | /** |
||
214 | * Test whether an object resource exists |
||
215 | * |
||
216 | * @param RepositoryPathInterface $path |
||
217 | * @return boolean Object resource exists |
||
218 | */ |
||
219 | 37 | public function objectResourceExists(RepositoryPathInterface $path) |
|
225 | } |
||
226 |