Test Failed
Push — master ( c2139f...3d9e00 )
by Paweł
56:29
created

WidgetModel::getTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Template Engine Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. 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\Bundle\TemplatesSystemBundle\Model;
16
17
use Doctrine\Common\Collections\ArrayCollection;
18
use SWP\Bundle\TemplatesSystemBundle\Widget\GoogleAdSenseWidgetHandler;
19
use SWP\Bundle\TemplatesSystemBundle\Widget\MenuWidgetHandler;
20
use SWP\Component\Common\Model\TimestampableInterface;
21
use SWP\Component\Storage\Model\PersistableInterface;
22
use SWP\Component\TemplatesSystem\Gimme\Model\WidgetModelInterface;
23
use SWP\Component\TemplatesSystem\Gimme\Widget\HtmlWidgetHandler;
24
25
/**
26
 * WidgetModel.
27
 */
28
class WidgetModel implements WidgetModelInterface, TimestampableInterface, PersistableInterface
29
{
30
    /**
31
     * @var int
32
     */
33
    protected $id;
34
35
    /**
36
     * @var string
37
     */
38
    protected $type;
39
40
    /**
41
     * @var string
42
     */
43
    protected $name;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $visible;
49
50
    /**
51
     * @var []
52
     */
53
    protected $parameters;
54
55
    /**
56
     * @var ArrayCollection
57
     */
58
    protected $containers;
59
60
    /**
61
     * @var \DateTime
62
     */
63
    protected $createdAt;
64
65
    /**
66
     * @var \DateTime
67
     */
68
    protected $updatedAt = null;
69
70
    /**
71
     * WidgetModel constructor.
72
     */
73
    public function __construct()
74
    {
75
        $this->createdAt = new \DateTime();
76
        $this->parameters = [];
77
        $this->setVisible();
78
        $this->setType();
79 14
    }
80
81 14
    /**
82 14
     * {@inheritdoc}
83 14
     */
84 14
    public function getTypes(): array
85 14
    {
86
        return [
87
            self::TYPE_HTML => HtmlWidgetHandler::class,
88
            self::TYPE_ADSENSE => GoogleAdSenseWidgetHandler::class,
89
            self::TYPE_MENU => MenuWidgetHandler::class,
90
        ];
91
    }
92 8
93
    /**
94 8
     * Get id.
95
     *
96
     * @return int
97
     */
98
    public function getId()
99
    {
100
        return $this->id;
101
    }
102
103
    /**
104
     * Set id.
105
     *
106
     * @param string $id
107
     *
108
     * @return WidgetModel
109
     */
110
    public function setId($id)
111
    {
112
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $id is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
113
114
        return $this;
115
    }
116
117
    /**
118 14
     * Set name.
119
     *
120 14
     * @param string $name
121
     *
122 14
     * @return WidgetModel
123
     */
124
    public function setName($name)
125
    {
126
        $this->name = $name;
127
128
        return $this;
129
    }
130 3
131
    /**
132 3
     * Get name.
133
     *
134
     * @return string
135
     */
136
    public function getName()
137
    {
138
        return $this->name;
139
    }
140 3
141
    /**
142 3
     * Gets the value of visible.
143
     *
144
     * @return bool
145
     */
146
    public function getVisible()
147
    {
148
        return $this->visible;
149
    }
150
151
    /**
152 14
     * Sets the value of visible.
153
     *
154 14
     * @param bool $visible the visible
155
     *
156 14
     * @return WidgetModel
157
     */
158
    public function setVisible($visible = true)
159
    {
160
        $this->visible = $visible;
161
162
        return $this;
163
    }
164 3
165
    /**
166 3
     * Gets the value of type.
167
     *
168
     * @return string
169
     */
170
    public function getType()
171
    {
172
        return $this->type;
173
    }
174
175
    /**
176 14
     * Sets the value of type.
177
     *
178 14
     * @param int $type the type
179 14
     *
180
     * @return WidgetModel
181 1
     */
182
    public function setType($type = self::TYPE_HTML)
183
    {
184 14
        if (array_key_exists($type, $this->getTypes())) {
185
            $this->type = $this->getTypes()[$type];
186
        } else {
187
            if (in_array($type, $this->getTypes())) {
188
                $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
The property $type was declared of type string, but $type is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
189
            } else {
190 3
                $this->type = $this->getTypes()[self::TYPE_HTML];
191
            }
192 3
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function getParameters()
201
    {
202 14
        return $this->parameters;
203
    }
204 14
205
    /**
206 14
     * Sets the value of parameters.
207
     *
208
     * @param array $parameters the parameters
209
     *
210
     * @return WidgetModel
211
     */
212
    public function setParameters(array $parameters = [])
213
    {
214 1
        $this->parameters = $parameters;
215
216 1
        return $this;
217
    }
218
219
    /**
220
     * Gets the value of containers.
221
     *
222
     * @return ArrayCollection
223
     */
224
    public function getContainers()
225
    {
226
        return $this->containers;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function getCreatedAt()
233
    {
234
        return $this->createdAt;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function setCreatedAt(\DateTime $createdAt)
241
    {
242
        $this->createdAt = $createdAt;
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function getUpdatedAt()
249
    {
250
        if (is_null($this->updatedAt)) {
251
            return $this->createdAt;
252
        }
253
254
        return $this->updatedAt;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function setUpdatedAt(\DateTime $updatedAt)
261
    {
262
        $this->updatedAt = $updatedAt;
263
    }
264
265
    /**
266
     * Sets the value of containers.
267
     *
268
     * @param ArrayCollection $containers the containers
269
     *
270
     * @return WidgetModel
271
     */
272
    protected function setContainers(ArrayCollection $containers)
273
    {
274
        $this->containers = $containers;
275
276
        return $this;
277
    }
278
}
279