Repository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
ccs 14
cts 14
cp 1
wmc 5
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 19 2
A instance() 0 9 2
A create() 0 5 1
1
<?php
2
3
/**
4
 * apparat/object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Infrastructure
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\Infrastructure\Repository;
38
39
use Apparat\Kernel\Tests\Kernel;
40
use Apparat\Object\Domain\Repository\Service;
41
use Apparat\Object\Domain\Repository\Repository as DomainRepository;
42
43
/**
44
 * Repository gateway
45
 *
46
 * @package Apparat\Object
47
 * @subpackage Apparat\Object\Infrastructure
48
 */
49
class Repository
50
{
51
    /**
52
     * Register a repository
53
     *
54
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
55
     * @param array $config Repository configuration
56
     * @return DomainRepository Repository instance
57
     * @throws InvalidArgumentException If the repository URL is invalid
58
     * @throws InvalidArgumentException If the repository configuration is empty
59
     * @api
60
     */
61 25
    public static function register($url, array $config)
62
    {
63
        // Normalize to local repository URL
64
        try {
65 25
            $url = Service::normalizeRepositoryUrl($url);
66 1
        } catch (\RuntimeException $e) {
67 1
            throw new InvalidArgumentException($e->getMessage(), $e->getCode());
68
        }
69
70
        // Instantiate the object repository
71
        /** @var DomainRepository $repository */
72 24
        $repository = Kernel::create(DomainRepository::class, [$url, $config]);
73
74
        // Register the repository
75 18
        Kernel::create(Service::class)->register($url, $repository);
76
77
        // Return the registered repository instance
78 18
        return $repository;
79
    }
80
81
    /**
82
     * Instantiate and return an object repository
83
     *
84
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
85
     * @return DomainRepository Object repository
86
     * @throws InvalidArgumentException If the repository URL is invalid
87
     * @throws InvalidArgumentException If the repository URL is unknown
88
     * @api
89
     */
90 36
    public static function instance($url)
91
    {
92
        // Normalize to return a repository instance matching this URL
93
        try {
94 36
            return Kernel::create(Service::class)->get($url);
95 3
        } catch (\Exception $e) {
96 3
            throw new InvalidArgumentException($e->getMessage(), $e->getCode());
97
        }
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 DomainRepository Repository instance
106
     * @api
107
     */
108 11
    public static function create($url, array $config)
109
    {
110 11
        $config['init'] = true;
111 11
        return self::register($url, $config);
112
    }
113
}
114