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( |
|
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 | 34 | public function loadObject(RepositoryLocatorInterface $locator, $visibility = SelectorInterface::ALL) |
|
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 | 35 | public function loadObjectResource( |
|
136 | RepositoryLocatorInterface &$currentLocator, |
||
137 | $visibility = SelectorInterface::ALL |
||
138 | ) { |
||
139 | // Validate the object visibility |
||
140 | 35 | $this->validateVisibility($visibility); |
|
141 | |||
142 | 34 | $objectResource = null; |
|
143 | |||
144 | // Create the current revision locators (visible and hidden) |
||
145 | /** @var RepositoryLocatorInterface[] $currentLocators */ |
||
146 | 34 | $currentLocators = array_filter([ |
|
147 | 34 | ($visibility & SelectorInterface::VISIBLE) ? $currentLocator->setHidden(false) : null, |
|
148 | 34 | ($visibility & SelectorInterface::HIDDEN) ? $currentLocator->setHidden(true) : null, |
|
149 | ]); |
||
150 | |||
151 | // Run through the possible revision locators |
||
152 | 34 | foreach ($currentLocators as $currentLocatorIndex => $currentLocator) { |
|
153 | try { |
||
154 | // Load the current object resource |
||
155 | 34 | $objectResource = $this->getObjectResource($currentLocator); |
|
156 | 33 | break; |
|
157 | |||
158 | // In case of an error |
||
159 | 3 | } catch (InvalidReaderArgumentException $e) { |
|
160 | // If it's not an error about the resource not existing or if it's the last possible option |
||
161 | 3 | if (($e->getCode() != InvalidReaderArgumentException::RESOURCE_DOES_NOT_EXIST) |
|
162 | 3 | || ($currentLocatorIndex >= (count($currentLocators) - 1)) |
|
163 | ) { |
||
164 | 3 | throw $e; |
|
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | // If the resource could not be loaded |
||
170 | 33 | if (!($objectResource instanceof ResourceInterface)) { |
|
171 | throw new InvalidArgumentException( |
||
172 | 'Resource does not exist', |
||
173 | InvalidArgumentException::RESOURCE_DOES_NOT_EXIST |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | 33 | return $objectResource; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Validate a given object visibility |
||
182 | * |
||
183 | * @param int $visibility Object visibility |
||
184 | * @throw RepositoryInvalidArgumentException If the visibility requirement is invalid |
||
185 | */ |
||
186 | 35 | protected function validateVisibility($visibility) |
|
201 | |||
202 | /** |
||
203 | * Instantiate object resource |
||
204 | * |
||
205 | * @param RepositoryLocatorInterface $locator |
||
206 | * @return ResourceInterface Object resource |
||
207 | */ |
||
208 | 34 | public function getObjectResource(RepositoryLocatorInterface $locator) |
|
214 | |||
215 | /** |
||
216 | * Test whether an object resource exists |
||
217 | * |
||
218 | * @param RepositoryLocatorInterface $locator |
||
219 | * @return boolean Object resource exists |
||
220 | */ |
||
221 | 38 | public function objectResourceExists(RepositoryLocatorInterface $locator) |
|
227 | } |
||
228 |