Completed
Push — master ( 299541...2cf242 )
by Joschi
03:39
created

RepositoryFacade::instance()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Collection;
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\Ports\Contract\ApparatObjectInterface;
46
use Apparat\Object\Ports\Factory\SelectorFactory;
47
use Apparat\Object\Ports\Repository\SelectorInterface;
48
49
/**
50
 * Repository facade
51
 *
52
 * @package Apparat\Object
53
 * @subpackage Apparat\Object\Ports
54
 */
55
class RepositoryFacade implements FacadeInterface
56
{
57
    /**
58
     * Repository
59
     *
60
     * @var RepositoryInterface
61
     */
62
    protected $repository;
63
64
    /**
65
     * Repository facade constructor
66
     *
67
     * @param RepositoryInterface $repository
68
     */
69 4
    protected function __construct(RepositoryInterface $repository)
70
    {
71 4
        $this->repository = $repository;
72 4
    }
73
74
    /**
75
     * Register a repository
76
     *
77
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
78
     * @param array $config Repository configuration
79
     * @return RepositoryFacade Repository facade
80
     * @api
81
     */
82 1
    public static function register($url, array $config)
83
    {
84 1
        return new static(\Apparat\Object\Infrastructure\Repository\Repository::register($url, $config));
85
    }
86
87
    /**
88
     * Instantiate and return an object repository
89
     *
90
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
91
     * @return RepositoryFacade Repository facade
92
     * @api
93
     */
94 3
    public static function instance($url)
95
    {
96 3
        return new static(\Apparat\Object\Infrastructure\Repository\Repository::instance($url));
97
    }
98
99
    /**
100
     * Create a repository
101
     *
102
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
103
     * @param array $config Repository configuration
104
     * @return RepositoryFacade Repository facade
105
     * @api
106
     */
107
    public static function create($url, array $config)
108
    {
109
        return new static(\Apparat\Object\Infrastructure\Repository\Repository::create($url, $config));
110
    }
111
112
    /**
113
     * Find objects by selector
114
     *
115
     * @param string|SelectorInterface $selector Object selector
116
     * @return Collection Object collection
117
     * @todo Cast collection as apparat objects
118
     */
119
    public function findObjects($selector)
120
    {
121
        if (!($selector instanceof SelectorInterface)) {
122
            $selector = SelectorFactory::createFromString($selector);
123
        }
124
        $collection = $this->repository->findObjects($selector);
125
        return $collection;
126
    }
127
128
    /**
129
     * Load an object from this repository
130
     *
131
     * @param string $locator Object locator
132
     * @param int $visibility Object visibility
133
     * @return ApparatObjectInterface Object
134
     */
135 3
    public function loadObject($locator, $visibility = SelectorInterface::ALL)
136
    {
137
        /** @var LocatorInterface $objectLocator */
138 3
        $objectLocator = Kernel::create(RepositoryLocator::class, [$this->repository, $locator]);
139 3
        return ApparatObjectFactory::create($this->repository->loadObject($objectLocator, $visibility));
140
    }
141
}
142