Passed
Push — master ( 6eb156...b34d93 )
by Thierry
04:25
created

ViewManager::getNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Ui\View;
4
5
use Jaxon\Di\Container;
6
use Jaxon\Utils\Config\Config;
7
8
use Closure;
9
10
use function array_filter;
11
use function is_array;
12
use function rtrim;
13
14
class ViewManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ViewManager
Loading history...
15
{
16
    /**
17
     * @var Container
18
     */
19
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
20
21
    /**
22
     * The view namespaces
23
     *
24
     * @var array
25
     */
26
    protected $aNamespaces = [];
27
28
    /**
29
     * The class constructor
30
     *
31
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
32
     */
33
    public function __construct(Container $di)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
34
    {
35
        $this->di = $di;
36
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
37
38
    /**
39
     * Add a view namespace, and set the corresponding renderer.
40
     *
41
     * @param string $sNamespace    The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
42
     * @param string $sDirectory    The namespace directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
43
     * @param string $sExtension    The extension to append to template names
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
44
     * @param string $sRenderer    The corresponding renderer name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
45
     *
46
     * @return void
47
     */
48
    public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension, string $sRenderer)
49
    {
50
        $aNamespace = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 21 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
            'directory' => $sDirectory,
52
            'extension' => $sExtension,
53
            'renderer' => $sRenderer,
54
        ];
55
        $this->aNamespaces[$sNamespace] = $aNamespace;
56
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
57
58
    /**
59
     * Set the view namespaces.
60
     *
61
     * @param Config $xAppConfig    The config options provided in the library
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
62
     * @param Config|null $xUserConfig    The config options provided in the app section of the global config file.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
63
     *
64
     * @return void
65
     */
66
    public function addNamespaces(Config $xAppConfig, ?Config $xUserConfig = null)
67
    {
68
        if(empty($aNamespaces = $xAppConfig->getOptionNames('views')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
69
        {
70
            return;
71
        }
72
        $sPackage = $xAppConfig->getOption('package', '');
73
        foreach($aNamespaces as $sNamespace => $sOption)
74
        {
75
            // Save the namespace
76
            $aNamespace = $xAppConfig->getOption($sOption);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
77
            $aNamespace['package'] = $sPackage;
78
            if(!isset($aNamespace['renderer']))
79
            {
80
                $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer.
81
            }
82
83
            // If the lib config has defined a template option, then its value must be
84
            // read from the app config.
85
            if($xUserConfig !== null && isset($aNamespace['template']) && is_array($aNamespace['template']))
86
            {
87
                $sTemplateOption = $xAppConfig->getOption($sOption . '.template.option');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
88
                $sTemplateDefault = $xAppConfig->getOption($sOption . '.template.default');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
89
                $sTemplate = $xUserConfig->getOption($sTemplateOption, $sTemplateDefault);
0 ignored issues
show
Bug introduced by
It seems like $sTemplateOption can also be of type null; however, parameter $sName of Jaxon\Utils\Config\Config::getOption() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
                $sTemplate = $xUserConfig->getOption(/** @scrutinizer ignore-type */ $sTemplateOption, $sTemplateDefault);
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
                $aNamespace['directory'] = rtrim($aNamespace['directory'], '/') . '/' . $sTemplate;
91
            }
92
93
            $this->aNamespaces[$sNamespace] = $aNamespace;
94
        }
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Get the view renderer
99
     *
100
     * @param string $sId    The unique identifier of the view renderer
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
101
     *
102
     * @return ViewInterface
103
     */
104
    public function getRenderer(string $sId): ViewInterface
105
    {
106
        // Return the view renderer with the given id
107
        return $this->di->g('jaxon.app.view.' . $sId);
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Add a view renderer with an id
112
     *
113
     * @param string $sId    The unique identifier of the view renderer
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
114
     * @param Closure $xClosure    A closure to create the view instance
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
115
     *
116
     * @return void
117
     */
118
    public function addRenderer(string $sId, Closure $xClosure)
119
    {
120
        // Return the initialized view renderer
121
        $this->di->set('jaxon.app.view.' . $sId, function($di) use ($sId, $xClosure) {
122
            // Get the defined renderer
123
            $xRenderer = $xClosure($di);
124
            // Init the renderer with the template namespaces
125
            $aNamespaces = array_filter($this->aNamespaces, function($aNamespace) use($sId) {
126
                return $aNamespace['renderer'] === $sId;
127
            });
128
            foreach($aNamespaces as $sNamespace => $aNamespace)
129
            {
130
                $xRenderer->addNamespace($sNamespace, $aNamespace['directory'], $aNamespace['extension']);
131
            }
132
            return $xRenderer;
133
        });
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Get the view renderer for a given namespace
138
     *
139
     * @param string $sNamespace    The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
140
     *
141
     * @return ViewInterface|null
142
     */
143
    public function getNamespaceRenderer(string $sNamespace): ?ViewInterface
144
    {
145
        if(!isset($this->aNamespaces[$sNamespace]))
146
        {
147
            return null;
148
        }
149
        // Return the view renderer with the configured id
150
        return $this->getRenderer($this->aNamespaces[$sNamespace]['renderer']);
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
152
}
153