Completed
Push — master ( 1c625d...234c6d )
by Iman
01:39
created

Normalizer::normalizeJsonWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
use Imanghafoori\Widgets\Utils\Normalizers\CacheNormalizer;
6
use Imanghafoori\Widgets\Utils\Normalizers\TemplateNormalizer;
7
use Imanghafoori\Widgets\Utils\Normalizers\PresenterNormalizer;
8
use Imanghafoori\Widgets\Utils\Normalizers\ControllerNormalizer;
9
10
class Normalizer
11
{
12
    private $presenterNormalizer;
13
    private $templateNormalizer;
14
    private $cacheNormalizer;
15
    private $controllerNormalizer;
16
17
    /**
18
     * Normalizer constructor which accepts dependencies.
19
     * @param TemplateNormalizer $templateNormalizer
20
     * @param CacheNormalizer $cacheNormalizer
21
     * @param PresenterNormalizer $presenterNormalizer
22
     * @param ControllerNormalizer $controllerNormalizer
23
     */
24
    public function __construct(
25
        TemplateNormalizer $templateNormalizer,
26
        CacheNormalizer $cacheNormalizer,
27
        PresenterNormalizer $presenterNormalizer,
28
        ControllerNormalizer $controllerNormalizer
29
    ) {
30
        $this->presenterNormalizer = $presenterNormalizer;
31
        $this->controllerNormalizer = $controllerNormalizer;
32
        $this->templateNormalizer = $templateNormalizer;
33
        $this->cacheNormalizer = $cacheNormalizer;
34
    }
35
36
    /**
37
 * Figures out and sets the widget configs according to conventions.
38
 * @param object $widget
39
 */
40
    public function normalizeWidgetConfig($widget)
41
    {
42
        // to avoid normalizing a widget multiple times unnecessarily :
43
        if (isset($widget->isNormalized)) {
44
            return;
45
        }
46
47
        $this->controllerNormalizer->normalizeControllerMethod($widget);
48
        $this->presenterNormalizer->normalizePresenterName($widget);
49
        $this->templateNormalizer->normalizeTemplateName($widget);
50
        $this->cacheNormalizer->normalizeCacheLifeTime($widget);
51
        $this->cacheNormalizer->normalizeCacheTags($widget);
52
        $this->normalizeContextAs($widget);
53
        $widget->isNormalized = true;
54
    }
55
56
    /**
57
     * Figures out and sets json widget configs according to conventions.
58
     * @param object $widget
59
     */
60
    public function normalizeJsonWidget($widget)
61
    {
62
        $this->controllerNormalizer->normalizeControllerMethod($widget);
63
        $this->cacheNormalizer->normalizeCacheLifeTime($widget);
64
        $this->cacheNormalizer->normalizeCacheTags($widget);
65
    }
66
67
    /**
68
     * Figures out what the variable name should be in view file.
69
     * @param object $widget
70
     * @return null
71
     */
72
    private function normalizeContextAs($widget)
73
    {
74
        $contextAs = 'data';
75
        if (property_exists($widget, 'contextAs')) {
76
            // removes the $ sign.
77
            $contextAs = str_replace('$', '', (string) $widget->contextAs);
78
        }
79
        $widget->contextAs = $contextAs;
80
    }
81
}
82