Completed
Push — master ( bf375b...cc5661 )
by Rafał
07:07
created

AbstractWidgetHandler::getModelParameter()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Gimme\Widget;
16
17
use SWP\Component\TemplatesSystem\Gimme\Model\WidgetModelInterface;
18
19
abstract class AbstractWidgetHandler implements WidgetHandlerInterface
20
{
21
    protected static $expectedParameters = [];
22
23
    protected $widgetModel;
24
25
    /**
26
     * @return array
27
     */
28
    public static function getExpectedParameters()
29
    {
30
        return static::$expectedParameters;
31
    }
32
33
    /**
34
     * AbstractWidgetHandler constructor.
35
     *
36
     * @param WidgetModelInterface $widgetModel
37
     */
38
    public function __construct(WidgetModelInterface $widgetModel)
39
    {
40
        $this->widgetModel = $widgetModel;
41
    }
42
43
    /**
44
     * @param $name
45
     *
46
     * @return string
47
     */
48
    protected function getModelParameter($name)
49
    {
50
        if (isset($this->widgetModel->getParameters()[$name])) {
51
            return $this->widgetModel->getParameters()[$name];
52
        }
53
54
        // Get default value
55
        if (isset(self::getExpectedParameters()[$name])) {
56
            $parameterMetaData = self::getExpectedParameters()[$name];
57
            if (is_array($parameterMetaData) && isset($parameterMetaData['default'])) {
58
                return $parameterMetaData['default'];
59
            }
60
        }
61
62
        return;
63
    }
64
65
    /**
66
     * Check if widget should be rendered.
67
     *
68
     * @return bool
69
     */
70
    public function isVisible()
71
    {
72
        return $this->widgetModel->getVisible();
73
    }
74
75
    /**
76
     * Returns associative array with all expected parameters and their values.
77
     *
78
     * @return array
79
     */
80
    protected function getAllParametersWithValue()
81
    {
82
        $all = array();
83
        foreach (self::getExpectedParameters() as $key => $value) {
84
            $all[$key] = $this->getModelParameter($key);
85
        }
86
87
        return $all;
88
    }
89
}
90