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 | 38 | public function createObject( |
|
103 | |||
104 | /** |
||
105 | * Load an object from a repository |
||
106 | * |
||
107 | * @param RepositoryLocatorInterface $locator Repository object locator |
||
108 | * @param int $visibility Object visibility |
||
109 | * @return ObjectInterface Object |
||
110 | */ |
||
111 | 33 | public function loadObject(RepositoryLocatorInterface $locator, $visibility = SelectorInterface::ALL) |
|
112 | { |
||
113 | // Create the current revision locator |
||
114 | /** @var RepositoryLocatorInterface $currentLocator */ |
||
115 | 33 | $currentLocator = $locator->setRevision(Revision::current()); |
|
116 | |||
117 | // Load the object resource respecting visibility constraints |
||
118 | 33 | $objectResource = $this->loadObjectResource($currentLocator, $visibility); |
|
119 | |||
120 | // Instantiate the object |
||
121 | 31 | $object = ObjectFactory::createFromResource($currentLocator, $objectResource); |
|
122 | |||
123 | // Use and return the requested object revision |
||
124 | 31 | return $object->useRevision($locator->getRevision()); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Load and return an object resource respecting visibility constraints |
||
129 | * |
||
130 | * @param RepositoryLocatorInterface $currentLocator |
||
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(RepositoryLocatorInterface &$currentLocator, $visibility = SelectorInterface::ALL) |
|
136 | { |
||
137 | // Validate the object visibility |
||
138 | 34 | $this->validateVisibility($visibility); |
|
139 | |||
140 | 33 | $objectResource = null; |
|
141 | |||
142 | // Create the current revision locators (visible and hidden) |
||
143 | /** @var RepositoryLocatorInterface[] $currentLocators */ |
||
144 | 33 | $currentLocators = array_filter([ |
|
145 | 33 | ($visibility & SelectorInterface::VISIBLE) ? $currentLocator->setHidden(false) : null, |
|
146 | 33 | ($visibility & SelectorInterface::HIDDEN) ? $currentLocator->setHidden(true) : null, |
|
147 | 33 | ]); |
|
148 | |||
149 | // Run through the possible revision locators |
||
150 | 33 | foreach ($currentLocators as $currentLocatorIndex => $currentLocator) { |
|
151 | try { |
||
152 | // Load the current object resource |
||
153 | 33 | $objectResource = $this->getObjectResource($currentLocator); |
|
154 | 32 | break; |
|
155 | |||
156 | // In case of an error |
||
157 | 3 | } catch (InvalidReaderArgumentException $e) { |
|
158 | // If it's not an error about the resource not existing or if it's the last possible option |
||
159 | 3 | if (($e->getCode() != InvalidReaderArgumentException::RESOURCE_DOES_NOT_EXIST) |
|
160 | 3 | || ($currentLocatorIndex >= (count($currentLocators) - 1)) |
|
161 | 3 | ) { |
|
162 | 1 | throw $e; |
|
163 | } |
||
164 | } |
||
165 | 33 | } |
|
166 | |||
167 | // If the resource could not be loaded |
||
168 | 32 | if (!($objectResource instanceof ResourceInterface)) { |
|
169 | throw new InvalidArgumentException( |
||
170 | 'Resource does not exist', |
||
171 | InvalidArgumentException::RESOURCE_DOES_NOT_EXIST |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | 32 | return $objectResource; |
|
176 | } |
||
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 RepositoryLocatorInterface $locator |
||
204 | * @return ResourceInterface Object resource |
||
205 | */ |
||
206 | 33 | public function getObjectResource(RepositoryLocatorInterface $locator) |
|
212 | |||
213 | /** |
||
214 | * Test whether an object resource exists |
||
215 | * |
||
216 | * @param RepositoryLocatorInterface $locator |
||
217 | * @return boolean Object resource exists |
||
218 | */ |
||
219 | 37 | public function objectResourceExists(RepositoryLocatorInterface $locator) |
|
225 | } |
||
226 |