1 | <?php |
||
52 | class Repository implements RepositoryInterface |
||
53 | { |
||
54 | /** |
||
55 | * Apparat base URL |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $url = null; |
||
60 | /** |
||
61 | * Adapter strategy |
||
62 | * |
||
63 | * @var AdapterStrategyInterface |
||
64 | */ |
||
65 | protected $adapterStrategy = null; |
||
66 | /** |
||
67 | * Instance specific object cache |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $objectCache = []; |
||
72 | |||
73 | /******************************************************************************* |
||
74 | * PUBLIC METHODS |
||
75 | *******************************************************************************/ |
||
76 | |||
77 | /** |
||
78 | * Repository constructor |
||
79 | * |
||
80 | * @param string $url Apparat base URL |
||
81 | * @param array $config Adapter strategy configuration |
||
82 | */ |
||
83 | 14 | public function __construct( |
|
90 | |||
91 | /** |
||
92 | * Initialize the repository |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | public function initialize() |
||
100 | |||
101 | /** |
||
102 | * Find objects by selector |
||
103 | * |
||
104 | * @param SelectorInterface $selector Object selector |
||
105 | * @return Collection Object collection |
||
106 | */ |
||
107 | 7 | public function findObjects(SelectorInterface $selector) |
|
111 | |||
112 | /** |
||
113 | * Create an object and add it to the repository |
||
114 | * |
||
115 | * @param string|Type $type Object type |
||
116 | * @param string $payload Object payload |
||
117 | * @param array $propertyData Object property data |
||
118 | * @return ObjectInterface Object |
||
119 | */ |
||
120 | 1 | public function createObject($type, $payload = '', array $propertyData = []) |
|
121 | { |
||
122 | // Instantiate the object type |
||
123 | 1 | if (!($type instanceof Type)) { |
|
124 | /** @var Type $type */ |
||
125 | 1 | $type = Kernel::create(Type::class, [$type]); |
|
126 | } |
||
127 | |||
128 | /** @var ManagerInterface $objectManager */ |
||
129 | 1 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
130 | 1 | return $objectManager->createObject($this, $type, $payload, $propertyData); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Delete and object from the repository |
||
135 | * |
||
136 | * @param ObjectInterface $object Object |
||
137 | * @return boolean Success |
||
138 | */ |
||
139 | public function deleteObject(ObjectInterface $object) |
||
143 | |||
144 | /** |
||
145 | * Update an object in the repository |
||
146 | * |
||
147 | * @param ObjectInterface $object Object |
||
148 | * @return bool Success |
||
149 | */ |
||
150 | public function updateObject(ObjectInterface $object) |
||
154 | |||
155 | /** |
||
156 | * Load an object from this repository |
||
157 | * |
||
158 | * @param PathInterface $path Object path |
||
159 | * @return ObjectInterface Object |
||
160 | */ |
||
161 | 16 | public function loadObject(PathInterface $path) |
|
162 | { |
||
163 | // TODO: Really OK to cache? (Immutability ...) |
||
164 | 16 | if (empty($this->objectCache[$path->getId()->getId()])) { |
|
165 | /** @var ManagerInterface $objectManager */ |
||
166 | 6 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
167 | |||
168 | /** @var RepositoryPathInterface $repositoryPath */ |
||
169 | 6 | $repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]); |
|
170 | |||
171 | 6 | $this->objectCache[$path->getId()->getId()] = $objectManager->loadObject($repositoryPath); |
|
172 | } |
||
173 | |||
174 | 14 | return $this->objectCache[$path->getId()->getId()]; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Return the repository's adapter strategy |
||
179 | * |
||
180 | * @return AdapterStrategyInterface Adapter strategy |
||
181 | */ |
||
182 | 7 | public function getAdapterStrategy() |
|
186 | |||
187 | /** |
||
188 | * Return the repository URL (relative to Apparat base URL) |
||
189 | * |
||
190 | * @return string Repository URL |
||
191 | */ |
||
192 | 2 | public function getUrl() |
|
196 | |||
197 | /** |
||
198 | * Return the repository size (number of objects in the repository) |
||
199 | * |
||
200 | * @return int Repository size |
||
201 | */ |
||
202 | 1 | public function getSize() |
|
206 | } |
||
207 |