Completed
Push — master ( 2ca34f...a721bd )
by Joschi
03:33
created

RepositoryFacade::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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\Repository\Repository;
41
use Apparat\Object\Domain\Repository\Service;
42
use Apparat\Object\Infrastructure\Repository\InvalidArgumentException;
43
44
/**
45
 * Repository facade
46
 *
47
 * @package Apparat\Object
48
 * @subpackage Apparat\Object\Ports
49
 */
50
class RepositoryFacade implements FacadeInterface
51
{
52
    /*******************************************************************************
53
     * PUBLIC METHODS
54
     *******************************************************************************/
55
56
    /**
57
     * Register a repository
58
     *
59
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
60
     * @param array $config Repository configuration
61
     * @return \Apparat\Object\Domain\Repository\Repository Repository instance
62
     * @throws InvalidArgumentException If the repository URL is invalid
63
     * @throws InvalidArgumentException If the repository configuration is empty
64
     * @api
65
     */
66 23
    public static function register($url, array $config)
67
    {
68
        // Normalize to local repository URL
69
        try {
70 23
            $url = Service::normalizeRepositoryUrl($url);
71 1
        } catch (\RuntimeException $e) {
72 1
            throw new InvalidArgumentException($e->getMessage(), $e->getCode());
73
        }
74
75
        // Instantiate the object repository
76 22
        $repository = Kernel::create(Repository::class, [$url, $config]);
77
78
        // Register the repository
79 16
        Kernel::create(Service::class)->register($url, $repository);
80
81
        // Return the registered repository instance
82 16
        return $repository;
83
    }
84
85
    /**
86
     * Instantiate and return an object repository
87
     *
88
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
89
     * @return \Apparat\Object\Domain\Repository\Repository Object repository
90
     * @throws InvalidArgumentException If the repository URL is invalid
91
     * @throws InvalidArgumentException If the repository URL is unknown
92
     * @api
93
     */
94 24
    public static function instance($url)
95
    {
96
        // Normalize to return a repository instance matching this URL
97
        try {
98 24
            return Kernel::create(Service::class)->get($url);
99 3
        } catch (\Exception $e) {
100 3
            throw new InvalidArgumentException($e->getMessage(), $e->getCode());
101
        }
102
    }
103
104
    /**
105
     * Create a repository
106
     *
107
     * @param string $url Repository URL (relative or absolute including the apparat base URL)
108
     * @param array $config Repository configuration
109
     * @return \Apparat\Object\Domain\Repository\Repository Repository instance
110
     * @api
111
     */
112 10
    public static function create($url, array $config)
113
    {
114 10
        $config['init'] = true;
115 10
        return self::register($url, $config);
116
    }
117
}
118