RepositoryFacade::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Ports
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Ports\Facades;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Model\Object\ObjectInterface;
41
use Apparat\Object\Domain\Model\Uri\LocatorInterface;
42
use Apparat\Object\Domain\Model\Uri\RepositoryLocator;
43
use Apparat\Object\Domain\Repository\RepositoryInterface;
44
use Apparat\Object\Infrastructure\Factory\ApparatObjectFactory;
45
use Apparat\Object\Infrastructure\Repository\Repository;
46
use Apparat\Object\Ports\Contract\ApparatObjectInterface;
47
use Apparat\Object\Ports\Factory\SelectorFactory;
48
use Apparat\Object\Ports\Repository\SelectorInterface;
49
50
/**
51
 * Repository facade
52
 *
53
 * @package Apparat\Object
54
 * @subpackage Apparat\Object\Ports
55
 */
56
class RepositoryFacade implements FacadeInterface
57
{
58
    /**
59
     * Repository
60
     *
61
     * @var RepositoryInterface
62
     */
63
    protected $repository;
64
65
    /**
66
     * Repository facade constructor
67
     *
68
     * @param RepositoryInterface $repository
69
     */
70 10
    protected function __construct(RepositoryInterface $repository)
71
    {
72 10
        $this->repository = $repository;
73 10
    }
74
75
    /**
76
     * Register a repository
77
     *
78
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
79
     * @param array $config Repository configuration
80
     * @return RepositoryFacade Repository facade
81
     * @api
82
     */
83 1
    public static function register($url, array $config)
84
    {
85 1
        return new static(Repository::register($url, $config));
86
    }
87
88
    /**
89
     * Instantiate and return an object repository
90
     *
91
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
92
     * @return RepositoryFacade Repository facade
93
     * @api
94
     */
95 8
    public static function instance($url)
96
    {
97 8
        return new static(Repository::instance($url));
98
    }
99
100
    /**
101
     * Create a repository
102
     *
103
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
104
     * @param array $config Repository configuration
105
     * @return RepositoryFacade Repository facade
106
     * @api
107
     */
108 1
    public static function create($url, array $config)
109
    {
110 1
        return new static(Repository::create($url, $config));
111
    }
112
113
    /**
114
     * Find objects by selector
115
     *
116
     * @param string|SelectorInterface $selector Object selector
117
     * @return array Objects
118
     */
119 1
    public function findObjects($selector)
120
    {
121 1
        if (!($selector instanceof SelectorInterface)) {
122 1
            $selector = SelectorFactory::createFromString($selector);
123
        }
124 1
        $objects = [];
125
        /** @var ObjectInterface $object */
126 1
        foreach ($this->repository->findObjects($selector) as $object) {
127 1
            $objects[] = ApparatObjectFactory::create($object);
128
        }
129 1
        return $objects;
130
    }
131
132
    /**
133
     * Load an object from this repository
134
     *
135
     * @param string $locator Object locator
136
     * @param int $visibility Object visibility
137
     * @return ApparatObjectInterface Object
138
     */
139 7
    public function loadObject($locator, $visibility = SelectorInterface::ALL)
140
    {
141
        /** @var LocatorInterface $objectLocator */
142 7
        $objectLocator = Kernel::create(RepositoryLocator::class, [$this->repository, $locator]);
143 7
        return ApparatObjectFactory::create($this->repository->loadObject($objectLocator, $visibility));
144
    }
145
}
146