Completed
Pull Request — 2.1 (#1191)
by Rafał
17:38 queued 08:23
created

Article::setPackageUpdatedAt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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())) {
0 ignored issues
show
Bug introduced by
The variable $article seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

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.

Loading history...
205
            return;
206
        }
207
208
        $entityManager = $event->getEntityManager();
209
        $article = $event->getObject();
0 ignored issues
show
Unused Code introduced by
$article is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
210
        $this->package->setUpdatedAt($updatedAt);
211
212
        $entityManager->flush();
213
    }
214
}
215