Passed
Push — master ( 8c2aef...71f170 )
by Jeroen
03:47 queued 23s
created

MediaGroupToUpdate::hasChangesInMediaItemImport()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace Backend\Modules\MediaLibraryImporter\Component;
4
5
use Backend\Modules\MediaLibrary\Domain\MediaGroup\MediaGroup;
6
use Backend\Modules\MediaLibrary\Domain\MediaGroupMediaItem\MediaGroupMediaItem;
7
use Backend\Modules\MediaLibrary\Domain\MediaItem\MediaItem;
8
use Backend\Modules\MediaLibraryImporter\Domain\MediaItemImport\MediaItemImport;
9
use Doctrine\Common\Collections\Collection;
10
11
final class MediaGroupToUpdate
12
{
13
    /** @var MediaGroup */
14
    protected $mediaGroup;
15
16
    /** @var array */
17
    protected $connectedItems = [];
18
19
    /** @var bool */
20
    protected $hasChanges = false;
21
22
    /**
23
     * @param MediaGroup $mediaGroup
24
     */
25
    public function __construct(MediaGroup $mediaGroup)
26
    {
27
        $this->mediaGroup = $mediaGroup;
28
    }
29
30
    /**
31
     * @param MediaItemImport $mediaItemImport
32
     */
33
    public function addMediaItemImport(MediaItemImport $mediaItemImport)
34
    {
35
        $this->addConnectedItem($mediaItemImport->getSequence(), $mediaItemImport->getMediaItem());
0 ignored issues
show
Bug introduced by
It seems like $mediaItemImport->getMediaItem() can be null; however, addConnectedItem() 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...
36
        if ($this->hasChangesInMediaItemImport($mediaItemImport)) {
37
            $this->hasChanges = true;
38
        }
39
    }
40
41
    /**
42
     * @param int $sequence
43
     * @param MediaItem $mediaItem
44
     */
45
    private function addConnectedItem(int $sequence, MediaItem $mediaItem)
46
    {
47
        $this->connectedItems[$sequence] = $mediaItem;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getConnectedItems(): array
54
    {
55
        $connectedItems = $this->connectedItems;
56
57
        // We sort the keys, so the sequence is correct
58
        ksort($connectedItems);
59
60
        // We return a new array, so we have a sequence with no gaps in between
61
        return array_values($connectedItems);
62
    }
63
64
    /**
65
     * @return MediaGroup
66
     */
67
    public function getMediaGroup(): MediaGroup
68
    {
69
        return $this->mediaGroup;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function hasChanges(): bool
76
    {
77
        return $this->hasChanges;
78
    }
79
80
    /**
81
     * @param MediaItemImport $mediaItemImport
82
     * @return bool
83
     */
84
    private function hasChangesInMediaItemImport(MediaItemImport $mediaItemImport): bool
85
    {
86
        if ($mediaItemImport->getMediaItem() === null) {
87
            return false;
88
        }
89
90
        // When imported, we do have changes
91
        if ($mediaItemImport->getStatus()->isImported()) {
92
            return true;
93
        }
94
95
        // When status == "existing", we must check other other variables
96
        return ($mediaItemImport->getStatus()->isExisting()
97
            && $this->hasChangesInConnectedItems($this->mediaGroup->getConnectedItems(), $mediaItemImport)
98
        );
99
    }
100
101
    /**
102
     * Has changed connected items check for possible changes
103
     *
104
     * @param Collection $connectedItems
105
     * @param MediaItemImport $mediaItemImport
106
     * @return bool
107
     */
108
    private function hasChangesInConnectedItems(
109
        Collection $connectedItems,
110
        MediaItemImport $mediaItemImport
111
    ): bool {
112
        $arrayWithChanges = $connectedItems->filter(function (MediaGroupMediaItem $connectedItem) use ($mediaItemImport) {
113
            // Stop here, because ID is not equal
114
            if ($connectedItem->getItem()->getId() !== $mediaItemImport->getMediaItem()->getId()) {
115
                return false;
116
            }
117
118
            // Stop here, because sequence equals
119
            if ($connectedItem->getSequence() === $mediaItemImport->getSequence()) {
120
                return false;
121
            }
122
123
            return true;
124
        });
125
126
        return !empty($arrayWithChanges);
127
    }
128
}
129