Completed
Push — master ( bb6905...021686 )
by Grégoire
18s queued 12s
created

src/Model/Gallery.php (3 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
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
    /**
61
     * {@inheritdoc}
62
     */
63
    public function __toString()
64
    {
65
        return $this->getName() ?: '-';
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function setName($name): void
72
    {
73
        $this->name = $name;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getName()
80
    {
81
        return $this->name;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function setEnabled($enabled): void
88
    {
89
        $this->enabled = $enabled;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getEnabled()
96
    {
97
        return $this->enabled;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function setUpdatedAt(\DateTime $updatedAt = null): void
104
    {
105
        $this->updatedAt = $updatedAt;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getUpdatedAt()
112
    {
113
        return $this->updatedAt;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function setCreatedAt(\DateTime $createdAt = null): void
120
    {
121
        $this->createdAt = $createdAt;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getCreatedAt()
128
    {
129
        return $this->createdAt;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function setDefaultFormat($defaultFormat): void
136
    {
137
        $this->defaultFormat = $defaultFormat;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getDefaultFormat()
144
    {
145
        return $this->defaultFormat;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function setGalleryItems($galleryItems): void
152
    {
153
        $this->galleryItems = new ArrayCollection();
154
155
        foreach ($galleryItems as $galleryItem) {
156
            $this->addGalleryItem($galleryItem);
157
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getGalleryItems()
164
    {
165
        return $this->galleryItems;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function addGalleryItem(GalleryItemInterface $galleryItem): void
172
    {
173
        $galleryItem->setGallery($this);
174
175
        $this->galleryItems[] = $galleryItem;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function setContext($context): void
182
    {
183
        $this->context = $context;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getContext()
190
    {
191
        return $this->context;
192
    }
193
194
    /**
195
     * Reorders $galleryHasMedia items based on their position.
196
     */
197
    public function reorderGalleryHasMedia()
198
    {
199
        if ($this->getGalleryHasMedias() && $this->getGalleryHasMedias() instanceof \IteratorAggregate) {
0 ignored issues
show
The method getGalleryHasMedias() does not seem to exist on object<Sonata\MediaBundle\Model\Gallery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
            // reorder
201
            $iterator = $this->getGalleryHasMedias()->getIterator();
0 ignored issues
show
The method getGalleryHasMedias() does not seem to exist on object<Sonata\MediaBundle\Model\Gallery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
203
            $iterator->uasort(static function ($a, $b) {
204
                if ($a->getPosition() === $b->getPosition()) {
205
                    return 0;
206
                }
207
208
                return $a->getPosition() > $b->getPosition() ? 1 : -1;
209
            });
210
211
            $this->setGalleryHasMedias($iterator);
0 ignored issues
show
The method setGalleryHasMedias() does not seem to exist on object<Sonata\MediaBundle\Model\Gallery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
        }
213
    }
214
}
215