View   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isExists() 0 3 1
A set() 0 3 1
A getName() 0 3 1
A forgeView() 0 7 1
A displayView() 0 4 1
A getPath() 0 19 4
A get() 0 4 2
A __invoke() 0 13 2
1
<?php
2
/**
3
 * View class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Views
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Views;
10
11
use EBloodBank\Themes;
12
use EBloodBank\Traits\AclTrait;
13
14
/**
15
 * View class
16
 *
17
 * @since 1.0
18
 */
19
class View
20
{
21
    use AclTrait;
22
23
    /**
24
     * @var string
25
     * @since 1.0
26
     */
27
    protected $name;
28
29
    /**
30
     * @var array
31
     * @since 1.0
32
     */
33
    protected $data;
34
35
    /**
36
     * @return void
37
     * @since 1.0
38
     */
39
    public function __construct($name, array $data = [])
40
    {
41
        $this->name = $name;
42
        $this->data = $data;
43
    }
44
45
    /**
46
     * @return View
47
     * @since 1.0
48
     */
49
    public function forgeView(string $name, array $data = [])
50
    {
51
        $data = array_merge($this->data, $data);
52
        $view = new self($name, $data);
53
        $view->setAcl($this->getAcl());
54
55
        return $view;
56
    }
57
58
    /**
59
     * @return void
60
     * @since 1.0
61
     */
62
    public function displayView(string $name, array $data = [])
63
    {
64
        $view = $this->forgeView($name, $data);
65
        $view();
66
    }
67
68
    /**
69
     * @return void
70
     * @since 1.0
71
     */
72
    public function set($key, $value)
73
    {
74
        $this->data[$key] = $value;
75
    }
76
77
    /**
78
     * @return bool
79
     * @since 1.0
80
     */
81
    public function isExists($key)
82
    {
83
        return isset($this->data[$key]);
84
    }
85
86
    /**
87
     * @return mixed
88
     * @since 1.0
89
     */
90
    public function get($key)
91
    {
92
        if ($this->isExists($key)) {
93
            return $this->data[$key];
94
        }
95
    }
96
97
    /**
98
     * @return string
99
     * @since 1.0
100
     */
101
    public function getName()
102
    {
103
        return $this->name;
104
    }
105
106
    /**
107
     * @return string
108
     * @since 1.0
109
     */
110
    public function getPath()
111
    {
112
        $templatePath = '';
113
        $currentTheme = Themes::getCurrentTheme();
114
115
        if (empty($currentTheme)) {
116
            return $templatePath;
117
        }
118
119
        $stacks = $currentTheme->getAvailableTemplateStacks();
120
121
        foreach ($stacks as $stackPath) {
122
            $templatePath = $stackPath . '/' . $this->getName() . '.php';
123
            if (file_exists($templatePath)) {
124
                break;
125
            }
126
        }
127
128
        return $templatePath;
129
    }
130
131
    /**
132
     * @return void
133
     * @since 1.0
134
     */
135
    public function __invoke()
136
    {
137
        if (is_readable($this->getPath())) {
138
            $data = $this->data;
139
            $data += [
140
                'context' => $this,
141
                'view'    => $this,
142
                'acl'     => $this->getAcl(),
143
            ];
144
145
            extract($data, EXTR_REFS);
146
147
            include $this->getPath();
148
        }
149
    }
150
}
151