1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Model; |
18
|
|
|
|
19
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
20
|
|
|
use SWP\Bundle\AnalyticsBundle\Model\ContentListsAwareTrait; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\Article as BaseArticle; |
22
|
|
|
use SWP\Component\GeoIP\Model\GeoIpPlaceInterface; |
23
|
|
|
use SWP\Component\GeoIP\Model\Place; |
24
|
|
|
use SWP\Component\MultiTenancy\Model\OrganizationAwareTrait; |
25
|
|
|
use SWP\Component\MultiTenancy\Model\TenantAwareTrait; |
26
|
|
|
use SWP\Component\Paywall\Model\PaywallSecuredTrait; |
27
|
|
|
|
28
|
|
|
class Article extends BaseArticle implements ArticleInterface, GeoIpPlaceInterface |
29
|
|
|
{ |
30
|
|
|
use TenantAwareTrait; |
31
|
|
|
use OrganizationAwareTrait; |
32
|
|
|
use PaywallSecuredTrait; |
33
|
|
|
use ContentListsAwareTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PackageInterface |
37
|
|
|
*/ |
38
|
|
|
protected $package; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $isPublishedFBIA = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ArticleStatisticsInterface |
47
|
|
|
*/ |
48
|
|
|
protected $articleStatistics; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ExternalArticleInterface |
52
|
|
|
*/ |
53
|
|
|
protected $externalArticle; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
protected $commentsCount = 0; |
59
|
|
|
|
60
|
|
|
/** @var Place */ |
61
|
|
|
protected $geoIpPlace; |
62
|
|
|
|
63
|
|
|
/** @var bool */ |
64
|
|
|
protected $isPublishedToAppleNews = false; |
65
|
|
|
|
66
|
|
|
/** @var AppleNewsArticleInterface|null */ |
67
|
|
|
protected $appleNewsArticle; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function setId($id) |
73
|
|
|
{ |
74
|
|
|
$this->id = $id; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getPackage(): ?PackageInterface |
81
|
|
|
{ |
82
|
|
|
return $this->package; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function setPackage(?PackageInterface $package) |
89
|
|
|
{ |
90
|
|
|
$this->package = $package; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function isPublishedFBIA(): bool |
97
|
|
|
{ |
98
|
|
|
return $this->isPublishedFBIA; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function setPublishedFBIA(bool $isPublished) |
105
|
|
|
{ |
106
|
|
|
$this->isPublishedFBIA = $isPublished; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getArticleStatistics(): ?ArticleStatisticsInterface |
113
|
|
|
{ |
114
|
|
|
return $this->articleStatistics; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function setArticleStatistics(ArticleStatisticsInterface $articleStatistics): void |
121
|
|
|
{ |
122
|
|
|
$articleStatistics->setArticle($this); |
123
|
|
|
$this->articleStatistics = $articleStatistics; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getExternalArticle(): ?ExternalArticleInterface |
130
|
|
|
{ |
131
|
|
|
return $this->externalArticle; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function setExternalArticle(ExternalArticleInterface $externalArticle): void |
138
|
|
|
{ |
139
|
|
|
$this->externalArticle = $externalArticle; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getPackageExternalData() |
143
|
|
|
{ |
144
|
|
|
if (null === $this->getPackage()->getExternalData()) { |
145
|
|
|
return []; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$data = []; |
149
|
|
|
foreach ($this->getPackage()->getExternalData() as $singleData) { |
150
|
|
|
$data[$singleData->getKey()] = $singleData->getValue(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $data; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getCommentsCount(): int |
157
|
|
|
{ |
158
|
|
|
if (null === $this->commentsCount) { |
159
|
|
|
return 0; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->commentsCount; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function setCommentsCount(int $commentsCount): void |
166
|
|
|
{ |
167
|
|
|
$this->commentsCount = $commentsCount; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getGeoIpPlaces(): array |
171
|
|
|
{ |
172
|
|
|
$places = $this->getPlaces(); |
173
|
|
|
|
174
|
|
|
$geoPlaces = []; |
175
|
|
|
foreach ($places as $place) { |
176
|
|
|
$geoPlaces[] = new Place($place['country'] ?? '', $place['state'] ?? ''); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $geoPlaces; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function isPublishedToAppleNews(): bool |
183
|
|
|
{ |
184
|
|
|
return $this->isPublishedToAppleNews; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function setPublishedToAppleNews(bool $isPublished): void |
188
|
|
|
{ |
189
|
|
|
$this->isPublishedToAppleNews = $isPublished; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getAppleNewsArticle(): ?AppleNewsArticleInterface |
193
|
|
|
{ |
194
|
|
|
return $this->appleNewsArticle; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setAppleNewsArticle(?AppleNewsArticleInterface $appleNewsArticle): void |
198
|
|
|
{ |
199
|
|
|
$this->appleNewsArticle = $appleNewsArticle; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function setPackageUpdatedAt(LifecycleEventArgs $event): void |
203
|
|
|
{ |
204
|
|
|
if (null === ($updatedAt = $article->getUpdatedAt())) { |
|
|
|
|
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$entityManager = $event->getEntityManager(); |
209
|
|
|
$article = $event->getObject(); |
|
|
|
|
210
|
|
|
$this->package->setUpdatedAt($updatedAt); |
211
|
|
|
|
212
|
|
|
$entityManager->flush(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.