AbstractSource   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 77
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0
rs 10
ccs 14
cts 14
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setNext() 0 6 1
A collect() 0 8 2
A get() 0 10 3
find() 0 1 ?
A vars() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the skeleton package.
7
 *
8
 * (c) Gennady Knyazkin <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Gennadyx\Skeleton\VarSource;
15
16
/**
17
 * Class AbstractSource
18
 *
19
 * @author Gennady Knyazkin <[email protected]>
20
 */
21
abstract class AbstractSource
22
{
23
    /**
24
     * @var AbstractSource
25
     */
26
    protected $next;
27
28
    /**
29
     * @var array
30
     */
31
    private static $vars = [];
32
33
    /**
34
     * Set next source
35
     *
36
     * @param AbstractSource $source
37
     *
38
     * @return AbstractSource
39
     */
40 2
    public function setNext(AbstractSource $source): AbstractSource
41
    {
42 2
        $this->next = $source;
43
44 2
        return $source;
45
    }
46
47
    /**
48
     * Find and collect variables from sources
49
     *
50
     * @param array $requiredVars
51
     *
52
     * @return array
53
     */
54 2
    final public function collect(array $requiredVars = []): array
55
    {
56 2
        foreach ($requiredVars as $requiredVar) {
57 2
            self::$vars[$requiredVar] = $this->get($requiredVar);
58
        }
59
60 2
        return self::$vars;
61
    }
62
63
    /**
64
     * Get variable value by name
65
     *
66
     * @param string $name
67
     *
68
     * @return string
69
     */
70 2
    final protected function get(string $name): string
71
    {
72 2
        $value = $this->find($name);
73
74 2
        if (null === $value) {
75 2
            $value = null !== $this->next ? $this->next->get($name) : '';
76
        }
77
78 2
        return $value;
79
    }
80
81
    /**
82
     * Find value for variable by name
83
     *
84
     * @param string $name
85
     *
86
     * @return string|null
87
     */
88
    abstract protected function find(string $name);
89
90
    /**
91
     * @return array
92
     */
93 2
    final protected static function vars(): array
94
    {
95 2
        return self::$vars;
96
    }
97
}
98