Passed
Push — master ( 0ab104...bfda64 )
by Jeroen
03:26
created

Domain/MediaItemImport/MediaItemImport.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Backend\Modules\MediaLibraryImporter\Domain\MediaItemImport;
4
5
use Backend\Modules\MediaLibrary\Domain\MediaFolder\MediaFolder;
6
use Backend\Modules\MediaLibrary\Domain\MediaGroup\MediaGroup;
7
use Backend\Modules\MediaLibrary\Domain\MediaItem\MediaItem;
8
use Doctrine\ORM\Mapping as ORM;
9
10
/**
11
 * MediaItemImport
12
 *
13
 * @ORM\Entity(repositoryClass="Backend\Modules\MediaLibraryImporter\Domain\MediaItemImport\MediaItemImportRepository")
14
 * @ORM\HasLifecycleCallbacks
15
 */
16
class MediaItemImport
17
{
18
    /**
19
     * @var string
20
     *
21
     * @ORM\Id()
22
     * @ORM\GeneratedValue(strategy="UUID")
23
     * @ORM\Column(type="guid")
24
     */
25
    protected $id;
26
27
    /**
28
     * @var Status
29
     *
30
     * @ORM\Column(type="media_item_import_status")
31
     */
32
    protected $status;
33
34
    /**
35
     * @var Method
36
     *
37
     * @ORM\Column(type="media_item_import_method")
38
     */
39
    protected $method;
40
41
    /**
42
     * @var int
43
     *
44
     * @ORM\Column(type="integer")
45
     */
46
    protected $sequence;
47
48
    /**
49
     * @var string
50
     *
51
     * @ORM\Column(type="string")
52
     */
53
    protected $path;
54
55
    /**
56
     * @var MediaGroup
57
     *
58
     * @ORM\ManyToOne(
59
     *      targetEntity="Backend\Modules\MediaLibrary\Domain\MediaGroup\MediaGroup",
60
     *      cascade="persist",
61
     *      fetch="EAGER"
62
     * )
63
     * @ORM\JoinColumn(
64
     *      name="mediaGroupId",
65
     *      referencedColumnName="id",
66
     *      onDelete="cascade",
67
     *      nullable=false
68
     * )
69
     */
70
    protected $mediaGroup;
71
72
    /**
73
     * @var MediaItem|null
74
     *
75
     * @ORM\ManyToOne(
76
     *      targetEntity="Backend\Modules\MediaLibrary\Domain\MediaItem\MediaItem",
77
     *      cascade="persist"
78
     * )
79
     * @ORM\JoinColumn(
80
     *      name="mediaItemId",
81
     *      referencedColumnName="id",
82
     *      onDelete="cascade"
83
     * )
84
     */
85
    protected $mediaItem;
86
87
    /**
88
     * @var \DateTime
89
     *
90
     * @ORM\Column(type="datetime")
91
     */
92
    protected $createdOn;
93
94
    /**
95
     * @var \DateTime
96
     *
97
     * @ORM\Column(type="datetime", nullable=true)
98
     */
99
    protected $executedOn;
100
101
    /**
102
     * @var \DateTime
103
     *
104
     * @ORM\Column(type="datetime", nullable=true)
105
     */
106
    protected $importedOn;
107
108
    /**
109
     * @var string
110
     *
111
     * @ORM\Column(type="text", nullable=true)
112
     */
113
    protected $errorMessage;
114
115
    /**
116
     * @param MediaGroup $mediaGroup
117
     * @param string $path
118
     * @param int $sequence
119
     * @param Method $method
120
     */
121
    private function __construct(
122
        MediaGroup $mediaGroup,
123
        string $path,
124
        int $sequence,
125
        Method $method
126
    ) {
127
        $this->mediaGroup = $mediaGroup;
128
        $this->path = $path;
129
        $this->sequence = $sequence;
130
        $this->method = $method;
131
    }
132
133
    /**
134
     * @param MediaItemImportDataTransferObject $mediaItemDataTransferObject
135
     * @return MediaItemImport
136
     */
137
    public static function fromDataTransferObject(
138
        MediaItemImportDataTransferObject $mediaItemDataTransferObject
139
    ): MediaItemImport {
140
        return new self(
141
            $mediaItemDataTransferObject->mediaGroup,
0 ignored issues
show
It seems like $mediaItemDataTransferObject->mediaGroup can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
142
            $mediaItemDataTransferObject->path,
143
            $mediaItemDataTransferObject->sequence,
144
            $mediaItemDataTransferObject->method
145
        );
146
    }
147
148
    /**
149
     * Gets the value of id.
150
     *
151
     * @return string
152
     */
153
    public function getId(): string
154
    {
155
        return $this->id;
156
    }
157
158
    /**
159
     * @return Status
160
     */
161
    public function getStatus(): Status
162
    {
163
        return $this->status;
164
    }
165
166
    public function changeStatusToError(string $errorMessage)
167
    {
168
        $this->errorMessage = $errorMessage;
169
        $this->status = Status::error();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Backend\Modules\MediaLi...mImport\Status::error() of type object<self> is incompatible with the declared type object<Backend\Modules\M...MediaItemImport\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
170
    }
171
172
    /**
173
     * @param MediaItem $mediaItem
174
     */
175
    public function changeStatusToExisting(MediaItem $mediaItem)
176
    {
177
        $this->errorMessage = null;
178
        $this->mediaItem = $mediaItem;
179
        $this->status = Status::existing();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Backend\Modules\MediaLi...port\Status::existing() of type object<self> is incompatible with the declared type object<Backend\Modules\M...MediaItemImport\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
180
    }
181
182
    /**
183
     * @param string $path
184
     * @param MediaFolder $mediaFolder
185
     * @param int $userId
186
     */
187
    public function changeStatusToImported(
188
        string $path,
189
        MediaFolder $mediaFolder,
190
        int $userId
191
    ) {
192
        $this->mediaItem = MediaItem::createFromLocalStorageType(
193
            $path,
194
            $mediaFolder,
195
            $userId
196
        );
197
198
        $this->errorMessage = null;
199
        $this->status = Status::imported();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Backend\Modules\MediaLi...port\Status::imported() of type object<self> is incompatible with the declared type object<Backend\Modules\M...MediaItemImport\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
200
        $this->importedOn = new \Datetime();
201
    }
202
203
    /**
204
     * @return Method
205
     */
206
    public function getMethod(): Method
207
    {
208
        return $this->method;
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    public function getPath(): string
215
    {
216
        return $this->path;
217
    }
218
219
    /**
220
     * @return int
221
     */
222
    public function getSequence(): int
223
    {
224
        return $this->sequence;
225
    }
226
227
    /**
228
     * Gets the value of createdOn.
229
     *
230
     * @return \DateTime
231
     */
232
    public function getCreatedOn(): \DateTime
233
    {
234
        return $this->createdOn;
235
    }
236
237
    /**
238
     * @return \DateTime
239
     */
240
    public function getImportedOn(): \DateTime
241
    {
242
        return $this->importedOn;
243
    }
244
245
    /**
246
     * @return \DateTime
247
     */
248
    public function getExecutedOn(): \DateTime
249
    {
250
        return $this->executedOn;
251
    }
252
253
    /**
254
     * @return MediaGroup
255
     */
256
    public function getMediaGroup(): MediaGroup
257
    {
258
        return $this->mediaGroup;
259
    }
260
261
    /**
262
     * @return MediaItem|null
263
     */
264
    public function getMediaItem()
265
    {
266
        return $this->mediaItem;
267
    }
268
269
    /**
270
     * @return string
271
     */
272
    public function getErrorMessage(): string
273
    {
274
        return $this->errorMessage;
275
    }
276
277
    /**
278
     * @ORM\PrePersist
279
     */
280
    public function onPrePersist()
281
    {
282
        $this->createdOn = new \Datetime();
283
    }
284
285
    /**
286
     * @ORM\PreUpdate
287
     */
288
    public function onPreUpdate()
289
    {
290
        $this->executedOn = new \Datetime();
291
    }
292
}
293