Completed
Push — master ( c9de9a...8fa92c )
by Joschi
06:37
created

Repository::createObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
crap 2
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
    /*******************************************************************************
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 19
    public function __construct(
78
        $url,
79
        array $config
80
    ) {
81 19
        $this->url = rtrim('/'.$url, '/');
82 19
        $this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config);
83 13
    }
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 4
    public function createObject($type, $payload = '', array $propertyData = [])
105
    {
106
        // Instantiate the object type
107 4
        if (!($type instanceof Type)) {
108
            /** @var Type $type */
109 4
            $type = Kernel::create(Type::class, [$type]);
110 4
        }
111
112
        /** @var ManagerInterface $objectManager */
113 4
        $objectManager = Kernel::create(Service::class)->getObjectManager();
114
115
        // Create and return the object
116 4
        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 1
    public function deleteObject(ObjectInterface $object)
126
    {
127 1
        $this->adapterStrategy->persistObject($object->delete());
128 1
        return true;
129
    }
130
131
    /**
132
     * Update an object in the repository
133
     *
134
     * @param ObjectInterface $object Object
135
     * @return bool Success
136
     */
137 3
    public function updateObject(ObjectInterface $object)
138
    {
139 3
        $this->adapterStrategy->persistObject($object);
140 1
        return true;
141
    }
142
143
    /**
144
     * Load an object from this repository
145
     *
146
     * @param PathInterface $path Object path
147
     * @param int $visibility Object visibility
148
     * @return ObjectInterface Object
149
     */
150 26
    public function loadObject(PathInterface $path, $visibility = SelectorInterface::ALL)
151
    {
152
        /** @var ManagerInterface $objectManager */
153 26
        $objectManager = Kernel::create(Service::class)->getObjectManager();
154
155
        /** @var RepositoryPathInterface $repositoryPath */
156 26
        $repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]);
157
158
        // Load and return the object
159 26
        return $objectManager->loadObject($repositoryPath, $visibility);
160
    }
161
162
    /**
163
     * Return the repository's adapter strategy
164
     *
165
     * @return AdapterStrategyInterface Adapter strategy
166
     */
167 29
    public function getAdapterStrategy()
168
    {
169 29
        return $this->adapterStrategy;
170
    }
171
172
    /**
173
     * Return the repository URL (relative to Apparat base URL)
174
     *
175
     * @return string Repository URL
176
     */
177 20
    public function getUrl()
178
    {
179 20
        return $this->url;
180
    }
181
182
    /**
183
     * Return the repository size (number of objects in the repository)
184
     *
185
     * @return int Repository size
186
     */
187 1
    public function getSize()
188
    {
189
        // TODO: Implement getSize() method.
190 1
    }
191
}
192