Completed
Push — master ( 1c58f0...a54299 )
by Rafał
06:29
created

AbstractWidgetHandler::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * {@inheritdoc}
45
     */
46
    public function isVisible()
47
    {
48
        return $this->widgetModel->getVisible();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getId()
55
    {
56
        return $this->widgetModel->getId();
57
    }
58
59
    /**
60
     * @param $name
61
     *
62
     * @return null|string
63
     */
64
    protected function getModelParameter($name)
65
    {
66
        if (isset($this->widgetModel->getParameters()[$name])) {
67
            return $this->widgetModel->getParameters()[$name];
68
        }
69
70
        // Get default value
71
        if (isset(self::getExpectedParameters()[$name])) {
72
            $parameterMetaData = self::getExpectedParameters()[$name];
73
            if (is_array($parameterMetaData) && isset($parameterMetaData['default'])) {
74
                return $parameterMetaData['default'];
75
            }
76
        }
77
78
        return;
79
    }
80
81
    /**
82
     * Returns associative array with all expected parameters and their values.
83
     *
84
     * @return array
85
     */
86
    protected function getAllParametersWithValue()
87
    {
88
        $all = array();
89
        foreach (self::getExpectedParameters() as $key => $value) {
90
            $all[$key] = $this->getModelParameter($key);
91
        }
92
93
        return $all;
94
    }
95
}
96