Completed
Push — master ( 88eef6...81252c )
by Joschi
02:44
created

Repository::loadObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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)
0 ignored issues
show
Coding Style introduced by
Spaces must be used for alignment; tabs are not allowed
Loading history...
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 10
    public function __construct(
81
        $url,
82
        array $config
83
    ) {
84 10
        $this->_url = rtrim('/'.$url, '/');
85 10
        $this->_adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config);
86 6
    }
87
88
    /**
89
     * Find objects by selector
90
     *
91
     * @param SelectorInterface $selector Object selector
92
     * @return Collection Object collection
93
     */
94 2
    public function findObjects(SelectorInterface $selector)
95
    {
96 2
        return new Collection($this->_adapterStrategy->findObjectPaths($selector, $this));
97
    }
98
99
    /**
100
     * Add an object to the repository
101
     *
102
     * @param ObjectInterface $object Object
103
     * @return boolean Success
104
     */
105
    public function addObject(ObjectInterface $object)
106
    {
107
        // TODO: Implement addObject() method.
108
    }
109
110
    /**
111
     * Delete and object from the repository
112
     *
113
     * @param ObjectInterface $object Object
114
     * @return boolean Success
115
     */
116
    public function deleteObject(ObjectInterface $object)
117
    {
118
        // TODO: Implement deleteObject() method.
119
    }
120
121
    /**
122
     * Update an object in the repository
123
     *
124
     * @param ObjectInterface $object Object
125
     * @return bool Success
126
     */
127
    public function updateObject(ObjectInterface $object)
128
    {
129
        // TODO: Implement updateObject() method.
130
    }
131
132
    /**
133
     * Load an object from this repository
134
     *
135
     * @param PathInterface $path Object path
136
     * @return ObjectInterface Object
137
     */
138 4
    public function loadObject(PathInterface $path)
139
    {
140
        // TODO: Really OK to cache? (Immutability ...)
141 4
        if (empty($this->_objectCache[$path->getId()->getId()])) {
142 2
            $this->_objectCache[$path->getId()->getId()] = Kernel::create(Service::class)->getObjectManager()->loadObject(
143 2
                new RepositoryPath(
144 2
                    $this,
145
                    $path
146 2
                )
147 2
            );
148 1
        }
149
150 3
        return $this->_objectCache[$path->getId()->getId()];
151
    }
152
153
    /**
154
     * Return the repository's adapter strategy
155
     *
156
     * @return AdapterStrategyInterface Adapter strategy
157
     */
158 2
    public function getAdapterStrategy()
159
    {
160 2
        return $this->_adapterStrategy;
161
    }
162
163
    /**
164
     * Return the repository URL (relative to Apparat base URL)
165
     *
166
     * @return string Repository URL
167
     */
168 1
    public function getUrl()
169
    {
170 1
        return $this->_url;
171
    }
172
}
173