1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object\Domain |
8
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
9
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/*********************************************************************************** |
14
|
|
|
* The MIT License (MIT) |
15
|
|
|
* |
16
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
19
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
20
|
|
|
* the Software without restriction, including without limitation the rights to |
21
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
22
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
23
|
|
|
* subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
30
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
31
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
32
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
33
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
34
|
|
|
***********************************************************************************/ |
35
|
|
|
|
36
|
|
|
namespace Apparat\Object\Domain\Repository; |
37
|
|
|
|
38
|
|
|
use Apparat\Kernel\Ports\Kernel; |
39
|
|
|
use Apparat\Object\Domain\Model\Object\Collection; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\ManagerInterface; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
42
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
43
|
|
|
use Apparat\Object\Domain\Model\Path\PathInterface; |
44
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPath; |
45
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Abstract object repository |
49
|
|
|
* |
50
|
|
|
* @package Apparat\Object\Domain\Repository |
51
|
|
|
*/ |
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
|
15 |
|
public function __construct( |
84
|
|
|
$url, |
85
|
|
|
array $config |
86
|
|
|
) { |
87
|
14 |
|
$this->url = rtrim('/'.$url, '/'); |
88
|
15 |
|
$this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config); |
89
|
10 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Initialize the repository |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function initialize() |
97
|
|
|
{ |
98
|
|
|
$this->adapterStrategy->initializeRepository(); |
99
|
|
|
} |
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) |
108
|
|
|
{ |
109
|
7 |
|
return Kernel::create(Collection::class, [$this->adapterStrategy->findObjectPaths($selector, $this)]); |
110
|
|
|
} |
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
|
1 |
|
} |
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) |
140
|
|
|
{ |
141
|
|
|
// TODO: Implement deleteObject() method. |
142
|
|
|
} |
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) |
151
|
|
|
{ |
152
|
|
|
// TODO: Implement updateObject() method. |
153
|
|
|
} |
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
|
|
|
|
166
|
|
|
/** @var ManagerInterface $objectManager */ |
167
|
6 |
|
$objectManager = Kernel::create(Service::class)->getObjectManager(); |
168
|
|
|
|
169
|
|
|
/** @var RepositoryPathInterface $repositoryPath */ |
170
|
6 |
|
$repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]); |
171
|
|
|
|
172
|
6 |
|
$this->objectCache[$path->getId()->getId()] = $objectManager->loadObject($repositoryPath); |
173
|
4 |
|
} |
174
|
|
|
|
175
|
14 |
|
return $this->objectCache[$path->getId()->getId()]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return the repository's adapter strategy |
180
|
|
|
* |
181
|
|
|
* @return AdapterStrategyInterface Adapter strategy |
182
|
|
|
*/ |
183
|
7 |
|
public function getAdapterStrategy() |
184
|
|
|
{ |
185
|
7 |
|
return $this->adapterStrategy; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return the repository URL (relative to Apparat base URL) |
190
|
|
|
* |
191
|
|
|
* @return string Repository URL |
192
|
|
|
*/ |
193
|
2 |
|
public function getUrl() |
194
|
|
|
{ |
195
|
2 |
|
return $this->url; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return the repository size (number of objects in the repository) |
200
|
|
|
* |
201
|
|
|
* @return int Repository size |
202
|
|
|
*/ |
203
|
1 |
|
public function getSize() |
204
|
|
|
{ |
205
|
|
|
// TODO: Implement getSize() method. |
206
|
1 |
|
} |
207
|
|
|
} |
208
|
|
|
|