Passed
Push — master ( d25cd5...5cf75b )
by Nikolaos
03:08
created

AbstractFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkService() 0 4 2
A init() 0 7 2
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