Completed
Push — master ( fbc7c8...750795 )
by Iman
02:06
created

Normalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
use Imanghafoori\Widgets\BaseWidget;
6
use Imanghafoori\Widgets\Utils\Normalizers\CacheNormalizer;
7
use Imanghafoori\Widgets\Utils\Normalizers\ControllerNormalizer;
8
use Imanghafoori\Widgets\Utils\Normalizers\PresenterNormalizer;
9
use Imanghafoori\Widgets\Utils\Normalizers\TemplateNormalizer;
10
11
class Normalizer
12
{
13
    private $widget;
14
    private $presenterNormalizer;
15
    private $templateNormalizer;
16
17
    /**
18
     * Normalizer constructor.
19
     * @param TemplateNormalizer $templateNormalizer
20
     * @param CacheNormalizer $cacheNormalizer
21
     * @param PresenterNormalizer $presenterNormalizer
22
     * @param ControllerNormalizer $controllerNormalizer
23
     * @internal param $widget
24
     */
25
    public function __construct(TemplateNormalizer $templateNormalizer, CacheNormalizer $cacheNormalizer, PresenterNormalizer $presenterNormalizer, ControllerNormalizer $controllerNormalizer)
26
    {
27
        $this->presenterNormalizer = $presenterNormalizer;
28
        $this->controllerNormalizer = $controllerNormalizer;
0 ignored issues
show
Bug introduced by
The property controllerNormalizer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
        $this->templateNormalizer = $templateNormalizer;
30
        $this->cacheNormalizer = $cacheNormalizer;
0 ignored issues
show
Bug introduced by
The property cacheNormalizer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
    }
32
33
34
    public function normalizeWidgetConfig(BaseWidget $widget)
35
    {
36
        // to avoid normalizing a widget multiple times unnecessarily :
37
        if (isset($widget->isNormalized)) {
38
            return null;
39
        }
40
41
        $this->widget = $widget;
42
        $this->controllerNormalizer->normalizeControllerMethod($widget);
43
        $this->presenterNormalizer->normalizePresenterName($widget);
44
        $this->templateNormalizer->normalizeTemplateName($widget);
45
        $this->cacheNormalizer->normalizeCacheLifeTime($widget);
46
        $this->cacheNormalizer->normalizeCacheTags($widget);
47
        $this->normalizeContextAs();
48
        $widget->isNormalized = true;
0 ignored issues
show
Bug introduced by
The property isNormalized does not seem to exist in Imanghafoori\Widgets\BaseWidget.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
    }
50
51
    /**
52
     * Figures out what the variable name should be in view file.
53
     * @return null
54
     */
55
    private function normalizeContextAs()
56
    {
57
        // removes the $ sign.
58
        $this->widget->contextAs = str_replace('$', '', (string)$this->widget->contextAs);
59
    }
60
}
61