Passed
Push — master ( 5cf75b...52ecf5 )
by Nikolaos
04:27 queued 01:52
created

AbstractFactory::checkService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Factory;
15
16
use function array_merge;
17
18
/**
19
 * Class AbstractFactory
20
 *
21
 * @property array $mapper
22
 */
23
abstract class AbstractFactory
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $mapper = [];
29
30
    /**
31
     * Checks if a service exists and throws an exception
32
     *
33
     * @param string $name
34
     *
35
     * @throws Exception
36
     */
37 122
    protected function checkService(string $name): void
38
    {
39 122
        if (!isset($this->mapper[$name])) {
40 3
            throw new Exception("Service " . $name . " is not registered");
41
        }
42 119
    }
43
44
    /**
45
     * Returns the adapters for the factory
46
     */
47
    abstract protected function getAdapters(): array;
48
49
    /**
50
     * AdapterFactory constructor.
51
     *
52
     * @param array $services
53
     */
54 190
    protected function init(array $services = []): void
55
    {
56 190
        $adapters = $this->getAdapters();
57 190
        $adapters = array_merge($adapters, $services);
58
59 190
        foreach ($adapters as $name => $service) {
60 190
            $this->mapper[$name] = $service;
61
        }
62 190
    }
63
}
64