Passed
Push — master ( 8ef4d4...d6939e )
by Victor
46s queued 11s
created

PresentationModel   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 76
rs 10
c 0
b 0
f 0
ccs 22
cts 22
cp 1
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A __construct() 0 3 1
A setVariables() 0 5 3
A getVariable() 0 3 1
A getVariables() 0 3 1
A variableExists() 0 7 2
A withVariables() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot;
5
6
/**
7
 * Holds the variables available to a view.
8
 */
9
class PresentationModel
10
{
11
    /**
12
     * @param mixed[] $variables
13
     */
14 22
    final public function __construct(array $variables = [])
15
    {
16 22
        $this->setVariables($variables);
17 21
    }
18
19
    /**
20
     * @return string The name of the presentation model.
21
     */
22 3
    final public function getName(): string
23
    {
24 3
        return static::class;
25
    }
26
27
    /**
28
     * @param string $variable
29
     * @param mixed  $default
30
     *
31
     * @return mixed
32
     */
33 3
    final public function getVariable(string $variable, $default = null)
34
    {
35 3
        return $this->$variable ?? $default;
36
    }
37
38
    /**
39
     * @return mixed[]
40
     */
41 13
    final public function getVariables(): array
42
    {
43 13
        return get_object_vars($this);
44
    }
45
46
    /**
47
     * @param mixed[] $variables
48
     *
49
     * @return PresentationModel
50
     */
51 4
    final public function withVariables(array $variables): self
52
    {
53 4
        $new = clone $this;
54 4
        $new->setVariables($variables);
55
56 4
        return $new;
57
    }
58
59
    /**
60
     * @param mixed[] $variables
61
     *
62
     * @return void
63
     */
64 22
    private function setVariables(array $variables)
65
    {
66 22
        foreach ($variables as $variable => $value) {
67 8
            if ($this->variableExists($variable)) {
68 7
                $this->$variable = $value;
69
            }
70
        }
71 21
    }
72
73
    /**
74
     * @param string $variable
75
     *
76
     * @return bool
77
     */
78 7
    private function variableExists(string $variable): bool
79
    {
80 7
        if (static::class === self::class) {
81 2
            return true;
82
        }
83
84 6
        return property_exists($this, $variable);
85
    }
86
}
87