Passed
Push — dependabot/composer/doctrine/d... ( bc1a80...bcc5a8 )
by
unknown
41:27 queued 36:06
created

src/Common/Doctrine/Entity/Meta.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Common\Doctrine\Entity;
4
5
use Backend\Core\Engine\Meta as BackendMeta;
6
use Common\Doctrine\ValueObject\SEOFollow;
7
use Common\Doctrine\ValueObject\SEOIndex;
8
use Doctrine\ORM\Mapping as ORM;
9
10
/**
11
 * @ORM\Table(name="meta", indexes={@ORM\Index(name="idx_url", columns={"url"})})
12
 * @ORM\Entity(repositoryClass="Common\Doctrine\Repository\MetaRepository")
13
 * @ORM\HasLifecycleCallbacks()
14
 */
15
class Meta
16
{
17
    /**
18
     * @var int
19
     *
20
     * @ORM\Id
21
     * @ORM\GeneratedValue(strategy="AUTO")
22
     * @ORM\Column(type="integer")
23
     */
24
    private $id;
25
26
    /**
27
     * @var string
28
     *
29
     * @ORM\Column(type="string", length=255)
30
     */
31
    private $keywords;
32
33
    /**
34
     * @var bool
35
     *
36
     * @ORM\Column(type="boolean", name="keywords_overwrite", options={"default" = false})
37
     */
38
    private $keywordsOverwrite;
39
40
    /**
41
     * @var string
42
     *
43
     * @ORM\Column(type="string", length=255)
44
     */
45
    private $description;
46
47
    /**
48
     * @var bool
49
     *
50
     * @ORM\Column(type="boolean", name="description_overwrite", options={"default" = false})
51
     */
52
    private $descriptionOverwrite;
53
54
    /**
55
     * @var string
56
     *
57
     * @ORM\Column(type="string", length=255)
58
     */
59
    private $title;
60
61
    /**
62
     * @var bool
63
     *
64
     * @ORM\Column(type="boolean", name="title_overwrite", options={"default" = false})
65
     */
66
    private $titleOverwrite;
67
68
    /**
69
     * @var string
70
     *
71
     * @ORM\Column(type="string", length=255)
72
     */
73
    private $url;
74
75
    /**
76
     * @var bool
77
     *
78
     * @ORM\Column(type="boolean", name="url_overwrite", options={"default" = false})
79
     */
80
    private $urlOverwrite;
81
82
    /**
83
     * @var string|null
84
     *
85
     * @ORM\Column(type="text", nullable=true)
86
     */
87
    private $custom;
88
89
    /**
90
     * @var array|null|string
91
     *
92
     * Only can be string during persisting or updating in the database as it then contains the serialised value
93
     *
94
     * @ORM\Column(type="text", nullable=true)
95
     */
96
    private $data;
97
98
    /**
99
     * @var SEOFollow|null
100
     *
101
     * @ORM\Column(type="seo_follow", name="seo_follow", nullable=true)
102
     */
103
    private $seoFollow;
104
105
    /**
106
     * @var SEOIndex|null
107
     *
108
     * @ORM\Column(type="seo_index", name="seo_index", nullable=true)
109
     */
110
    private $seoIndex;
111
112
    public function __construct(
113
        string $keywords,
114
        bool $keywordsOverwrite,
115
        string $description,
116
        bool $descriptionOverwrite,
117
        string $title,
118
        bool $titleOverwrite,
119
        string $url,
120
        bool $urlOverwrite,
121
        string $custom = null,
122
        SEOFollow $seoFollow = null,
123
        SEOIndex $seoIndex = null,
124
        array $data = [],
125
        int $id = null
126
    ) {
127
        $this->keywords = $keywords;
128
        $this->keywordsOverwrite = $keywordsOverwrite;
129
        $this->description = $description;
130
        $this->descriptionOverwrite = $descriptionOverwrite;
131
        $this->title = $title;
132
        $this->titleOverwrite = $titleOverwrite;
133
        $this->url = $url;
134
        $this->urlOverwrite = $urlOverwrite;
135
        $this->custom = $custom;
136
        $this->data = $data;
137
        $this->seoFollow = $seoFollow;
138
        $this->seoIndex = $seoIndex;
139
        $this->id = $id;
140
    }
141
142
    public function update(
143
        string $keywords,
144
        bool $keywordsOverwrite,
145
        string $description,
146
        bool $descriptionOverwrite,
147
        string $title,
148
        bool $titleOverwrite,
149
        string $url,
150
        bool $urlOverwrite,
151
        string $custom = null,
152
        SEOFollow $seoFollow = null,
153
        SEOIndex $seoIndex = null,
154
        array $data = []
155
    ) {
156
        $this->keywords = $keywords;
157
        $this->keywordsOverwrite = $keywordsOverwrite;
158
        $this->description = $description;
159
        $this->descriptionOverwrite = $descriptionOverwrite;
160
        $this->title = $title;
161
        $this->titleOverwrite = $titleOverwrite;
162
        $this->url = $url;
163
        $this->urlOverwrite = $urlOverwrite;
164
        $this->custom = $custom;
165
        $this->data = $data;
166
        $this->seoFollow = $seoFollow;
167
        $this->seoIndex = $seoIndex;
168
    }
169
170
    /**
171
     * @ORM\PrePersist
172
     * @ORM\PreUpdate
173
     */
174
    public function serialiseData()
175
    {
176
        if (!empty($this->data)) {
177
            $this->data = serialize($this->data);
178
179
            return;
180
        }
181
182
        $this->data = null;
183
    }
184
185
    /**
186
     * @ORM\PostPersist
187
     * @ORM\PostUpdate
188
     * @ORM\PostLoad
189
     */
190
    public function unSerialiseData()
191
    {
192
        if ($this->data === null) {
193
            $this->data = [];
194
195
            return;
196
        }
197
198
        $this->data = unserialize($this->data, ['allowed_classes' => false]);
0 ignored issues
show
It seems like $this->data can also be of type array; however, parameter $str of unserialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
        $this->data = unserialize(/** @scrutinizer ignore-type */ $this->data, ['allowed_classes' => false]);
Loading history...
199
    }
200
201
    public static function fromBackendMeta(BackendMeta $meta): self
202
    {
203
        $metaData = $meta->getData();
204
205
        return new self(
206
            $metaData['keywords'],
207
            $metaData['keywords_overwrite'],
208
            $metaData['description'],
209
            $metaData['description_overwrite'],
210
            $metaData['title'],
211
            $metaData['title_overwrite'],
212
            $metaData['url'],
213
            $metaData['url_overwrite'],
214
            $metaData['custom'],
215
            array_key_exists('SEOFollow', $metaData) ? SEOFollow::fromString((string) $metaData['SEOFollow']) : null,
216
            array_key_exists('SEOIndex', $metaData) ? SEOIndex::fromString((string) $metaData['SEOIndex']) : null,
217
            $metaData['data'] ?? [],
218
            $meta->getId()
219
        );
220
    }
221
222
    /**
223
     * Used in the transformer of the Symfony form type for this entity
224
     *
225
     * @param array $metaData
226
     *
227
     * @return self
228
     */
229
    public static function updateWithFormData(array $metaData): self
230
    {
231
        return new self(
232
            $metaData['keywords'],
233
            $metaData['keywordsOverwrite'],
234
            $metaData['description'],
235
            $metaData['descriptionOverwrite'],
236
            $metaData['title'],
237
            $metaData['titleOverwrite'],
238
            $metaData['url'],
239
            $metaData['urlOverwrite'],
240
            $metaData['custom'] ?? null,
241
            SEOFollow::fromString((string) $metaData['SEOFollow']),
242
            SEOIndex::fromString((string) $metaData['SEOIndex']),
243
            [],
244
            (int) $metaData['id']
245
        );
246
    }
247
248
    public function getId(): int
249
    {
250
        return $this->id;
251
    }
252
253
    public function getKeywords(): string
254
    {
255
        return $this->keywords;
256
    }
257
258
    public function isKeywordsOverwrite(): bool
259
    {
260
        return $this->keywordsOverwrite;
261
    }
262
263
    public function getDescription(): string
264
    {
265
        return $this->description;
266
    }
267
268
    public function isDescriptionOverwrite(): bool
269
    {
270
        return $this->descriptionOverwrite;
271
    }
272
273
    public function getTitle(): string
274
    {
275
        return $this->title;
276
    }
277
278
    public function isTitleOverwrite(): bool
279
    {
280
        return $this->titleOverwrite;
281
    }
282
283
    public function getUrl(): string
284
    {
285
        return $this->url;
286
    }
287
288
    public function isUrlOverwrite(): bool
289
    {
290
        return $this->urlOverwrite;
291
    }
292
293
    public function getCustom(): ?string
294
    {
295
        return $this->custom;
296
    }
297
298
    public function getData(): array
299
    {
300
        return $this->data;
301
    }
302
303
    public function hasSEOIndex(): bool
304
    {
305
        return $this->seoIndex instanceof SEOIndex && !$this->seoIndex->isNone();
306
    }
307
308
    public function getSEOIndex(): ?SEOIndex
309
    {
310
        if (!$this->hasSEOIndex()) {
311
            return null;
312
        }
313
314
        return $this->seoIndex;
315
    }
316
317
    public function hasSEOFollow(): bool
318
    {
319
        return $this->seoFollow instanceof SEOFollow && !$this->seoFollow->isNone();
320
    }
321
322
    public function getSEOFollow(): ?SEOFollow
323
    {
324
        if (!$this->hasSEOFollow()) {
325
            return null;
326
        }
327
328
        return $this->seoFollow;
329
    }
330
}
331