Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Manager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 4 1
A getRenderers() 0 4 1
A getNamespaces() 0 4 1
A addNamespace() 0 17 2
A addNamespaces() 0 20 4
A getRenderer() 0 10 2
A addRenderer() 0 21 3
1
<?php
2
3
namespace Jaxon\Utils\View;
4
5
use Jaxon\Utils\Config\Config;
6
7
use stdClass;
8
use Exception;
9
use Closure;
10
11
class Manager
12
{
13
    /**
14
     * The default namespace
15
     *
16
     * @var string
17
     */
18
    protected $sDefaultNamespace = '';
19
20
    /**
21
     * The view renderers
22
     *
23
     * @var array
24
     */
25
    protected $aRenderers = [];
26
27
    /**
28
     * The view namespaces
29
     *
30
     * @var array
31
     */
32
    protected $aNamespaces = [];
33
34
    /**
35
     * Get the default namespace
36
     *
37
     * @return string
38
     */
39
    public function getDefaultNamespace()
40
    {
41
        return $this->sDefaultNamespace;
42
    }
43
44
    /**
45
     * Get the view renderers
46
     *
47
     * @return array
48
     */
49
    public function getRenderers()
50
    {
51
        return $this->aRenderers;
52
    }
53
54
    /**
55
     * Get the view namespaces
56
     *
57
     * @return array
58
     */
59
    public function getNamespaces()
60
    {
61
        return $this->aNamespaces;
62
    }
63
64
    /**
65
     * Add a view namespace, and set the corresponding renderer.
66
     *
67
     * @param string        $sNamespace         The namespace name
68
     * @param string        $sDirectory         The namespace directory
69
     * @param string        $sExtension         The extension to append to template names
70
     * @param string        $sRenderer          The corresponding renderer name
71
     *
72
     * @return void
73
     */
74
    public function addNamespace($sNamespace, $sDirectory, $sExtension, $sRenderer)
75
    {
76
        $aNamespace = [
77
            'namespace' => $sNamespace,
78
            'directory' => $sDirectory,
79
            'extension' => $sExtension,
80
        ];
81
        if(key_exists($sRenderer, $this->aNamespaces))
82
        {
83
            $this->aNamespaces[$sRenderer][] = $aNamespace;
84
        }
85
        else
86
        {
87
            $this->aNamespaces[$sRenderer] = [$aNamespace];
88
        }
89
        $this->aRenderers[$sNamespace] = $sRenderer;
90
    }
91
92
    /**
93
     * Set the view namespaces.
94
     *
95
     * @param Config            $xAppConfig             The application config options
96
     *
97
     * @return void
98
     */
99
    public function addNamespaces($xAppConfig)
100
    {
101
        $this->sDefaultNamespace = $xAppConfig->getOption('options.views.default', false);
102
        if(is_array($namespaces = $xAppConfig->getOptionNames('views')))
103
        {
104
            foreach($namespaces as $namespace => $option)
105
            {
106
                // If no default namespace is defined, use the first one as default.
107
                if($this->sDefaultNamespace == false)
108
                {
109
                    $this->sDefaultNamespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespace can also be of type integer. However, the property $sDefaultNamespace is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
110
                }
111
                // Save the namespace
112
                $directory = $xAppConfig->getOption($option . '.directory');
113
                $extension = $xAppConfig->getOption($option . '.extension', '');
114
                $renderer = $xAppConfig->getOption($option . '.renderer', 'jaxon');
115
                $this->addNamespace($namespace, $directory, $extension, $renderer);
116
            }
117
        }
118
    }
119
120
    /**
121
     * Get the view renderer facade
122
     *
123
     * @param string                $sId                The unique identifier of the view renderer
124
     *
125
     * @return object        The view renderer
126
     */
127
    public function getRenderer($sId = '')
128
    {
129
        if(!$sId)
130
        {
131
            // Return the view renderer facade
132
            return jaxon()->di()->get(\Jaxon\Utils\View\Renderer::class);
133
        }
134
        // Return the view renderer with the given id
135
        return jaxon()->di()->get('jaxon.app.view.' . $sId);
136
    }
137
138
    /**
139
     * Add a view renderer with an id
140
     *
141
     * @param string                $sId                The unique identifier of the view renderer
142
     * @param Closure               $xClosure           A closure to create the view instance
143
     *
144
     * @return void
145
     */
146
    public function addRenderer($sId, $xClosure)
147
    {
148
        // Return the non-initialiazed view renderer
149
        jaxon()->di()->set('jaxon.app.view.base.' . $sId, $xClosure);
0 ignored issues
show
Documentation introduced by
$xClosure is of type object<Closure>, but the function expects a object<Jaxon\Utils\DI\Closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
151
        // Return the initialized view renderer
152
        jaxon()->di()->set('jaxon.app.view.' . $sId, function ($c) use ($sId) {
0 ignored issues
show
Documentation introduced by
function ($c) use($sId) ... return $renderer; } is of type object<Closure>, but the function expects a object<Jaxon\Utils\DI\Closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
            // Get the defined renderer
154
            $renderer = $c['jaxon.app.view.base.' . $sId];
155
156
            // Init the renderer with the template namespaces
157
            if(key_exists($sId, $this->aNamespaces))
158
            {
159
                foreach($this->aNamespaces[$sId] as $ns)
160
                {
161
                    $renderer->addNamespace($ns['namespace'], $ns['directory'], $ns['extension']);
162
                }
163
            }
164
            return $renderer;
165
        });
166
    }
167
}
168