Repository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 146
rs 10
c 0
b 0
f 0
ccs 28
cts 28
cp 1
wmc 10
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 4 1
A __construct() 0 7 1
A findObjects() 0 5 1
A createObject() 0 18 2
A deleteObject() 0 5 1
A updateObject() 0 5 1
A loadObject() 0 11 1
A getAdapterStrategy() 0 4 1
A getSize() 0 4 1
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\Uri\LocatorInterface;
44
use Apparat\Object\Domain\Model\Uri\RepositoryLocator;
45
use Apparat\Object\Domain\Model\Uri\RepositoryLocatorInterface;
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
    /*******************************************************************************
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 24
    public function __construct(
78
        $url,
79
        array $config
80
    ) {
81 24
        $this->url = rtrim('/'.$url, '/');
82 24
        $this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config);
83 18
    }
84
85
    /**
86
     * Find objects by selector
87
     *
88
     * @param SelectorInterface $selector Object selector
89
     * @return Collection Object collection
90
     */
91 8
    public function findObjects(SelectorInterface $selector)
92
    {
93 8
        return Kernel::create(Collection::class,
94 8
            [$this->adapterStrategy->findObjectResourceLocators($selector, $this)]);
95
    }
96
97
    /**
98
     * Create an object and add it to the repository
99
     *
100
     * @param string|Type $type Object type
101
     * @param string $payload Object payload
102
     * @param array $propertyData Object property data
103
     * @param \DateTimeInterface $creationDate Object creation date
104
     * @return ObjectInterface Object
105
     */
106 8
    public function createObject(
107
        $type,
108
        $payload = '',
109
        array $propertyData = [],
110
        \DateTimeInterface $creationDate = null
111
    ) {
112
        // Instantiate the object type
113 8
        if (!($type instanceof Type)) {
114
            /** @var Type $type */
115 8
            $type = Kernel::create(Type::class, [$type]);
116
        }
117
118
        /** @var ManagerInterface $objectManager */
119 8
        $objectManager = Kernel::create(Service::class)->getObjectManager();
120
121
        // Create and return the object
122 8
        return $objectManager->createObject($this, $type, $payload, $propertyData, $creationDate);
123
    }
124
125
    /**
126
     * Delete and object from the repository
127
     *
128
     * @param ObjectInterface $object Object
129
     * @return boolean Success
130
     */
131 1
    public function deleteObject(ObjectInterface $object)
132
    {
133 1
        $this->adapterStrategy->persistObject($object->delete());
134 1
        return true;
135
    }
136
137
    /**
138
     * Update an object in the repository
139
     *
140
     * @param ObjectInterface $object Object
141
     * @return bool Success
142
     */
143 5
    public function updateObject(ObjectInterface $object)
144
    {
145 5
        $this->adapterStrategy->persistObject($object);
146 3
        return true;
147
    }
148
149
    /**
150
     * Load an object from this repository
151
     *
152
     * @param LocatorInterface $locator Object locator
153
     * @param int $visibility Object visibility
154
     * @return ObjectInterface Object
155
     */
156 42
    public function loadObject(LocatorInterface $locator, $visibility = SelectorInterface::ALL)
157
    {
158
        /** @var ManagerInterface $objectManager */
159 42
        $objectManager = Kernel::create(Service::class)->getObjectManager();
160
161
        /** @var RepositoryLocatorInterface $repositoryLocator */
162 42
        $repositoryLocator = Kernel::create(RepositoryLocator::class, [$this, $locator]);
163
164
        // Load and return the object
165 42
        return $objectManager->loadObject($repositoryLocator, $visibility);
166
    }
167
168
    /**
169
     * Return the repository's adapter strategy
170
     *
171
     * @return AdapterStrategyInterface Adapter strategy
172
     */
173 48
    public function getAdapterStrategy()
174
    {
175 48
        return $this->adapterStrategy;
176
    }
177
178
    /**
179
     * Return the repository URL (relative to Apparat base URL)
180
     *
181
     * @return string Repository URL
182
     */
183 35
    public function getUrl()
184
    {
185 35
        return $this->url;
186
    }
187
188
    /**
189
     * Return the repository size (number of objects in the repository)
190
     *
191
     * @return int Repository size
192
     */
193 1
    public function getSize()
194
    {
195
        // TODO: Implement getSize() method.
196 1
    }
197
}
198