Completed
Push — master ( 3bf76e...9f9c26 )
by Joschi
04:22
created

Repository::loadObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 13
Bugs 0 Features 2
Metric Value
c 13
b 0
f 2
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 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\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 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
        $this->adapterStrategy->persistObject($object);
139
        return true;
140
    }
141
142
    /**
143
     * Load an object from this repository
144
     *
145
     * @param PathInterface $path Object path
146
     * @return ObjectInterface Object
147
     */
148 24
    public function loadObject(PathInterface $path)
149
    {
150
        /** @var ManagerInterface $objectManager */
151 24
        $objectManager = Kernel::create(Service::class)->getObjectManager();
152
153
        /** @var RepositoryPathInterface $repositoryPath */
154 24
        $repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]);
155
156
        // Load and return the object
157 24
        return $objectManager->loadObject($repositoryPath);
158
    }
159
160
    /**
161
     * Return the repository's adapter strategy
162
     *
163
     * @return AdapterStrategyInterface Adapter strategy
164
     */
165 26
    public function getAdapterStrategy()
166
    {
167 26
        return $this->adapterStrategy;
168
    }
169
170
    /**
171
     * Return the repository URL (relative to Apparat base URL)
172
     *
173
     * @return string Repository URL
174
     */
175 5
    public function getUrl()
176
    {
177 5
        return $this->url;
178
    }
179
180
    /**
181
     * Return the repository size (number of objects in the repository)
182
     *
183
     * @return int Repository size
184
     */
185 1
    public function getSize()
186
    {
187
        // TODO: Implement getSize() method.
188 1
    }
189
}
190