Passed
Push — master ( 12e656...9e9627 )
by Nikolaos
02:33
created

AbstractFactory::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 330
    protected function checkService(string $name): void
38
    {
39 330
        if (!isset($this->mapper[$name])) {
40 10
            throw new Exception("Service " . $name . " is not registered");
41
        }
42 320
    }
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 468
    protected function init(array $services = []): void
55
    {
56 468
        $adapters = $this->getAdapters();
57 468
        $adapters = array_merge($adapters, $services);
58
59 468
        foreach ($adapters as $name => $service) {
60 468
            $this->mapper[$name] = $service;
61
        }
62 468
    }
63
}
64