Completed
Push — master ( 827038...ef4825 )
by Joschi
03:24
created

Repository::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\ObjectInterface;
41
use Apparat\Object\Domain\Model\Path\PathInterface;
42
use Apparat\Object\Domain\Model\Path\RepositoryPath;
43
44
/**
45
 * Abstract object repository
46
 *
47
 * @package Apparat\Object\Domain\Repository
48
 */
49
class Repository implements RepositoryInterface
50
{
51
    /**
52
     * Apparat base URL
53
     *
54
     * @var string
55
     */
56
    protected $url = null;
57
    /**
58
     * Adapter strategy
59
     *
60
     * @var AdapterStrategyInterface
61
     */
62
    protected $adapterStrategy = null;
63
    /**
64
     * Instance specific object cache
65
     *
66
     * @var array
67
     */
68
    protected $objectCache = [];
69
70
    /*******************************************************************************
71
     * PUBLIC METHODS
72
     *******************************************************************************/
73
74
    /**
75
     * Repository constructor
76
     *
77
     * @param string $url Apparat base URL
78
     * @param array $config Adapter strategy configuration
79
     */
80 12
    public function __construct(
81
        $url,
82
        array $config
83
    ) {
84 12
        $this->url = rtrim('/'.$url, '/');
85 12
        $this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config);
86 8
    }
87
88
    /**
89
     * Initialize the repository
90
     *
91
     * @return void
92
     */
93
    public function initialize()
94
    {
95
        $this->adapterStrategy->initializeRepository();
96
    }
97
98
    /**
99
     * Find objects by selector
100
     *
101
     * @param SelectorInterface $selector Object selector
102
     * @return Collection Object collection
103
     */
104 7
    public function findObjects(SelectorInterface $selector)
105
    {
106 7
        return Kernel::create(Collection::class, [$this->adapterStrategy->findObjectPaths($selector, $this)]);
107
    }
108
109
    /**
110
     * Add an object to the repository
111
     *
112
     * @param ObjectInterface $object Object
113
     * @return boolean Success
114
     */
115
    public function addObject(ObjectInterface $object)
116
    {
117
        // TODO: Implement addObject() method.
118
    }
119
120
    /**
121
     * Delete and object from the repository
122
     *
123
     * @param ObjectInterface $object Object
124
     * @return boolean Success
125
     */
126
    public function deleteObject(ObjectInterface $object)
127
    {
128
        // TODO: Implement deleteObject() method.
129
    }
130
131
    /**
132
     * Update an object in the repository
133
     *
134
     * @param ObjectInterface $object Object
135
     * @return bool Success
136
     */
137
    public function updateObject(ObjectInterface $object)
138
    {
139
        // TODO: Implement updateObject() method.
140
    }
141
142
    /**
143
     * Load an object from this repository
144
     *
145
     * @param PathInterface $path Object path
146
     * @return ObjectInterface Object
147
     */
148 16
    public function loadObject(PathInterface $path)
149
    {
150
        // TODO: Really OK to cache? (Immutability ...)
151 16
        if (empty($this->objectCache[$path->getId()->getId()])) {
152 6
            $this->objectCache[$path->getId()->getId()] =
153 6
                Kernel::create(Service::class)->getObjectManager()->loadObject(
154 6
                    Kernel::create(RepositoryPath::class, [$this, $path])
155
                );
156
        }
157
158 14
        return $this->objectCache[$path->getId()->getId()];
159
    }
160
161
    /**
162
     * Return the repository's adapter strategy
163
     *
164
     * @return AdapterStrategyInterface Adapter strategy
165
     */
166 6
    public function getAdapterStrategy()
167
    {
168 6
        return $this->adapterStrategy;
169
    }
170
171
    /**
172
     * Return the repository URL (relative to Apparat base URL)
173
     *
174
     * @return string Repository URL
175
     */
176 2
    public function getUrl()
177
    {
178 2
        return $this->url;
179
    }
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()
187
    {
188
        // TODO: Implement getSize() method.
189 1
    }
190
}
191