Completed
Push — master ( b81f06...ddd2b8 )
by Florian
11s
created

Registry::registerServices()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 21
Code Lines 16

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 16
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of Swap.
5
 *
6
 * (c) Florian Voutzinos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Swap\Service;
13
14
use Exchanger\Service\CentralBankOfCzechRepublic;
15
use Exchanger\Service\CentralBankOfRepublicTurkey;
16
use Exchanger\Service\CurrencyLayer;
17
use Exchanger\Service\EuropeanCentralBank;
18
use Exchanger\Service\Fixer;
19
use Exchanger\Service\Google;
20
use Exchanger\Service\NationalBankOfRomania;
21
use Exchanger\Service\OpenExchangeRates;
22
use Exchanger\Service\PhpArray;
23
use Exchanger\Service\WebserviceX;
24
use Exchanger\Service\Xignite;
25
use Exchanger\Service\Yahoo;
26
27
/**
28
 * Holds services.
29
 *
30
 * @author Florian Voutzinos <[email protected]>
31
 */
32
class Registry
33
{
34
    /**
35
     * The registered services.
36
     *
37
     * @var array
38
     */
39
    private static $services = [];
40
41
    /**
42
     * Constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->registerServices();
47
    }
48
49
    /**
50
     * Tells of the registry has the given service.
51
     *
52
     * @param string $name The service name
53
     *
54
     * @return bool
55
     */
56
    public function has($name)
57
    {
58
        return isset(self::$services[$name]);
59
    }
60
61
    /**
62
     * Gets a service.
63
     *
64
     * @param string $name The service name
65
     *
66
     * @return string|null
67
     */
68
    public function get($name)
69
    {
70
        return isset(self::$services[$name]) ? self::$services[$name] : null;
71
    }
72
73
    /**
74
     * Registers a new service.
75
     *
76
     * @param string          $name            The service name
77
     * @param string|callable $classOrCallable The class name or a callable
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81
    public static function register($name, $classOrCallable)
82
    {
83
        self::$services[$name] = $classOrCallable;
84
    }
85
86
    /**
87
     * Registers the core services.
88
     */
89
    private function registerServices()
90
    {
91
        $services = [
92
            'central_bank_of_czech_republic' => CentralBankOfCzechRepublic::class,
93
            'central_bank_of_republic_turkey' => CentralBankOfRepublicTurkey::class,
94
            'currencylayer' => CurrencyLayer::class,
95
            'european_central_bank' => EuropeanCentralBank::class,
96
            'fixer' => Fixer::class,
97
            'google' => Google::class,
98
            'national_bank_of_romania' => NationalBankOfRomania::class,
99
            'open_exchange_rates' => OpenExchangeRates::class,
100
            'array' => PhpArray::class,
101
            'webservicex' => WebserviceX::class,
102
            'xignite' => Xignite::class,
103
            'yahoo' => Yahoo::class,
104
        ];
105
106
        foreach ($services as $name => $class) {
107
            self::register($name, $class);
108
        }
109
    }
110
}
111