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 | /******************************************************************************* |
||
68 | * PUBLIC METHODS |
||
69 | *******************************************************************************/ |
||
70 | |||
71 | /** |
||
72 | * Repository constructor |
||
73 | * |
||
74 | * @param string $url Apparat base URL |
||
75 | * @param array $config Adapter strategy configuration |
||
76 | */ |
||
77 | 17 | public function __construct( |
|
78 | $url, |
||
79 | array $config |
||
80 | ) { |
||
81 | 17 | $this->url = rtrim('/'.$url, '/'); |
|
82 | 17 | $this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config); |
|
83 | 11 | } |
|
84 | |||
85 | /** |
||
86 | * Find objects by selector |
||
87 | * |
||
88 | * @param SelectorInterface $selector Object selector |
||
89 | * @return Collection Object collection |
||
90 | */ |
||
91 | 7 | public function findObjects(SelectorInterface $selector) |
|
92 | { |
||
93 | 7 | return Kernel::create(Collection::class, [$this->adapterStrategy->findObjectPaths($selector, $this)]); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Create an object and add it to the repository |
||
98 | * |
||
99 | * @param string|Type $type Object type |
||
100 | * @param string $payload Object payload |
||
101 | * @param array $propertyData Object property data |
||
102 | * @return ObjectInterface Object |
||
103 | */ |
||
104 | 2 | public function createObject($type, $payload = '', array $propertyData = []) |
|
105 | { |
||
106 | // Instantiate the object type |
||
107 | 2 | if (!($type instanceof Type)) { |
|
108 | /** @var Type $type */ |
||
109 | 2 | $type = Kernel::create(Type::class, [$type]); |
|
110 | 2 | } |
|
111 | |||
112 | /** @var ManagerInterface $objectManager */ |
||
113 | 2 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
114 | |||
115 | // Create and return the object |
||
116 | 2 | return $objectManager->createObject($this, $type, $payload, $propertyData); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Delete and object from the repository |
||
121 | * |
||
122 | * @param ObjectInterface $object Object |
||
123 | * @return boolean Success |
||
124 | */ |
||
125 | public function deleteObject(ObjectInterface $object) |
||
126 | { |
||
127 | // TODO: Implement deleteObject() method. |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Update an object in the repository |
||
132 | * |
||
133 | * @param ObjectInterface $object Object |
||
134 | * @return bool Success |
||
135 | */ |
||
136 | public function updateObject(ObjectInterface $object) |
||
137 | { |
||
138 | echo 'persisting'; |
||
139 | |||
140 | return true; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Load an object from this repository |
||
145 | * |
||
146 | * @param PathInterface $path Object path |
||
147 | * @return ObjectInterface Object |
||
148 | */ |
||
149 | 19 | public function loadObject(PathInterface $path) |
|
150 | { |
||
151 | /** @var ManagerInterface $objectManager */ |
||
152 | 19 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
153 | |||
154 | /** @var RepositoryPathInterface $repositoryPath */ |
||
155 | 19 | $repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]); |
|
156 | |||
157 | // Load and return the object |
||
158 | 19 | return $objectManager->loadObject($repositoryPath); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Return the repository's adapter strategy |
||
163 | * |
||
164 | * @return AdapterStrategyInterface Adapter strategy |
||
165 | */ |
||
166 | 21 | public function getAdapterStrategy() |
|
167 | { |
||
168 | 21 | return $this->adapterStrategy; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Return the repository URL (relative to Apparat base URL) |
||
173 | * |
||
174 | * @return string Repository URL |
||
175 | */ |
||
176 | 14 | public function getUrl() |
|
180 | |||
181 | /** |
||
182 | * Return the repository size (number of objects in the repository) |
||
183 | * |
||
184 | * @return int Repository size |
||
185 | */ |
||
186 | 1 | public function getSize() |
|
190 | } |
||
191 |