Completed
Push — master ( a721bd...6a60a9 )
by Joschi
03:34
created

Repository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

3 Methods

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