Completed
Push — master ( 0d3940...d66330 )
by Jordi Sala
02:12
created

Gallery::reorderGalleryItems()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sonata\MediaBundle\Provider\MediaProviderInterface;
19
20
/**
21
 * NEXT_MAJOR: remove GalleryMediaCollectionInterface interface. Move its content into GalleryInterface.
22
 */
23
abstract class Gallery implements GalleryInterface, GalleryMediaCollectionInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $context;
29
30
    /**
31
     * @var string
32
     */
33
    protected $name;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $enabled;
39
40
    /**
41
     * @var \DateTime|null
42
     */
43
    protected $updatedAt;
44
45
    /**
46
     * @var \DateTime|null
47
     */
48
    protected $createdAt;
49
50
    /**
51
     * @var string
52
     */
53
    protected $defaultFormat = MediaProviderInterface::FORMAT_REFERENCE;
54
55
    /**
56
     * @var GalleryItemInterface[]|Collection
57
     */
58
    protected $galleryItems;
59
60
    public function __toString()
61
    {
62
        return $this->getName() ?: '-';
63
    }
64
65
    public function setName($name): void
66
    {
67
        $this->name = $name;
68
    }
69
70
    public function getName()
71
    {
72
        return $this->name;
73
    }
74
75
    public function setEnabled($enabled): void
76
    {
77
        $this->enabled = $enabled;
78
    }
79
80
    public function getEnabled()
81
    {
82
        return $this->enabled;
83
    }
84
85
    public function setUpdatedAt(?\DateTime $updatedAt = null): void
86
    {
87
        $this->updatedAt = $updatedAt;
88
    }
89
90
    public function getUpdatedAt()
91
    {
92
        return $this->updatedAt;
93
    }
94
95
    public function setCreatedAt(?\DateTime $createdAt = null): void
96
    {
97
        $this->createdAt = $createdAt;
98
    }
99
100
    public function getCreatedAt()
101
    {
102
        return $this->createdAt;
103
    }
104
105
    public function setDefaultFormat($defaultFormat): void
106
    {
107
        $this->defaultFormat = $defaultFormat;
108
    }
109
110
    public function getDefaultFormat()
111
    {
112
        return $this->defaultFormat;
113
    }
114
115
    public function setGalleryItems($galleryItems): void
116
    {
117
        $this->galleryItems = new ArrayCollection();
118
119
        foreach ($galleryItems as $galleryItem) {
120
            $this->addGalleryItem($galleryItem);
121
        }
122
    }
123
124
    public function getGalleryItems()
125
    {
126
        return $this->galleryItems;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->galleryItems; of type Sonata\MediaBundle\Model...\Collections\Collection adds the type Doctrine\Common\Collections\Collection to the return on line 126 which is incompatible with the return type declared by the interface Sonata\MediaBundle\Model...erface::getGalleryItems of type Sonata\MediaBundle\Model\GalleryItemInterface[].
Loading history...
127
    }
128
129
    public function addGalleryItem(GalleryItemInterface $galleryItem): void
130
    {
131
        $galleryItem->setGallery($this);
132
133
        $this->galleryItems[] = $galleryItem;
134
    }
135
136
    public function setContext($context): void
137
    {
138
        $this->context = $context;
139
    }
140
141
    public function getContext()
142
    {
143
        return $this->context;
144
    }
145
146
    /**
147
     * Reorders $galleryItems based on their position.
148
     */
149
    public function reorderGalleryItems()
150
    {
151
        if ($this->getGalleryItems() && $this->getGalleryItems() instanceof \IteratorAggregate) {
152
            // reorder
153
            $iterator = $this->getGalleryItems()->getIterator();
154
155
            $iterator->uasort(static function ($a, $b) {
156
                if ($a->getPosition() === $b->getPosition()) {
157
                    return 0;
158
                }
159
160
                return $a->getPosition() > $b->getPosition() ? 1 : -1;
161
            });
162
163
            $this->setGalleryItems($iterator);
0 ignored issues
show
Documentation introduced by
$iterator is of type object<Traversable>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164
        }
165
    }
166
}
167