Completed
Push — master ( 81c425...a2bf82 )
by Joschi
02:41
created

Repository::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 14
    public function __construct(
78
        $url,
79
        array $config
80
    ) {
81 14
        $this->url = rtrim('/'.$url, '/');
82 14
        $this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config);
83 10
    }
84
85
    /**
86
     * Initialize the repository
87
     *
88
     * @return void
89
     */
90
    public function initialize()
91
    {
92
        $this->adapterStrategy->initializeRepository();
93
    }
94
95
    /**
96
     * Find objects by selector
97
     *
98
     * @param SelectorInterface $selector Object selector
99
     * @return Collection Object collection
100
     */
101 8
    public function findObjects(SelectorInterface $selector)
102
    {
103 8
        return Kernel::create(Collection::class, [$this->adapterStrategy->findObjectPaths($selector, $this)]);
104
    }
105
106
    /**
107
     * Create an object and add it to the repository
108
     *
109
     * @param string|Type $type Object type
110
     * @param string $payload Object payload
111
     * @param array $propertyData Object property data
112
     * @return ObjectInterface Object
113
     */
114 1
    public function createObject($type, $payload = '', array $propertyData = [])
115
    {
116
        // Instantiate the object type
117 1
        if (!($type instanceof Type)) {
118
            /** @var Type $type */
119 1
            $type = Kernel::create(Type::class, [$type]);
120 1
        }
121
122
        /** @var ManagerInterface $objectManager */
123 1
        $objectManager = Kernel::create(Service::class)->getObjectManager();
124
125
        // Create and return the object
126 1
        return $objectManager->createObject($this, $type, $payload, $propertyData);
127
    }
128
129
    /**
130
     * Delete and object from the repository
131
     *
132
     * @param ObjectInterface $object Object
133
     * @return boolean Success
134
     */
135
    public function deleteObject(ObjectInterface $object)
136
    {
137
        // TODO: Implement deleteObject() method.
138
    }
139
140
    /**
141
     * Update an object in the repository
142
     *
143
     * @param ObjectInterface $object Object
144
     * @return bool Success
145
     */
146
    public function updateObject(ObjectInterface $object)
147
    {
148
        // TODO: Implement updateObject() method.
149
    }
150
151
    /**
152
     * Load an object from this repository
153
     *
154
     * @param PathInterface $path Object path
155
     * @return ObjectInterface Object
156
     */
157 16
    public function loadObject(PathInterface $path)
158
    {
159
        /** @var ManagerInterface $objectManager */
160 16
        $objectManager = Kernel::create(Service::class)->getObjectManager();
161
162
        /** @var RepositoryPathInterface $repositoryPath */
163 16
        $repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]);
164
165
        // Load and return the object
166 16
        return $objectManager->loadObject($repositoryPath);
167
    }
168
169
    /**
170
     * Return the repository's adapter strategy
171
     *
172
     * @return AdapterStrategyInterface Adapter strategy
173
     */
174 17
    public function getAdapterStrategy()
175
    {
176 17
        return $this->adapterStrategy;
177
    }
178
179
    /**
180
     * Return the repository URL (relative to Apparat base URL)
181
     *
182
     * @return string Repository URL
183
     */
184 11
    public function getUrl()
185
    {
186 11
        return $this->url;
187
    }
188
189
    /**
190
     * Return the repository size (number of objects in the repository)
191
     *
192
     * @return int Repository size
193
     */
194 1
    public function getSize()
195
    {
196
        // TODO: Implement getSize() method.
197 1
    }
198
}
199