Completed
Push — develop ( a72fbd...d809fa )
by
unknown
09:25
created

ImageSet::__call()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Entity;
12
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
16
17
/**
18
 * Manages a set of images which belongs together.
19
 *
20
 * @ODM\EmbeddedDocument
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @since 0.29
23
 */
24
class ImageSet implements ImageSetInterface
25
{
26
    use EntityTrait;
27
28
    /**
29
     * The id of this image set.
30
     *
31
     * @ODM\Field(type="string")
32
     * @var string
33
     */
34
    protected $id;
35
36
    /**
37
     * Images in this set.
38
     *
39
     * @ODM\ReferenceMany(discriminatorField="_entity", cascade="all", orphanRemoval=true)
40
     * @var Collection
41
     */
42
    protected $images;
43
44
    /**
45
     * @internal
46
     *      Creates a unique {@link $id} for this ImageSet
47
     */
48
    public function __construct()
49
    {
50
        $this->id = (string) new \MongoId();
51
    }
52
53
    /**
54
     * Provide convinient methods for get and set.
55
     *
56
     * - get<ImageKey> proxies to get(<imageKey>)
57
     * - set<ImageKey>($image) proxies to set(<imageKey>, $image)
58
     *
59
     * @param string $method
60
     * @param array $args
61
     *
62
     * @return $this|ImageInterface|null
63
     * @throws \BadMethodCallException
64
     */
65
    public function __call($method, $args)
66
    {
67
        if (0 === strpos($method, 'get')) {
68
            $key = lcfirst(substr($method, 3));
69
            $fallback = count($args) ? $args[0] : true;
70
71
            return $this->get($key, $fallback);
72
        }
73
74
        if (0 === strpos($method, 'set')) {
75
            if (1 > count($args)) {
76
                throw new \BadMethodCallException(sprintf(
77
                    'Missing argument 1 for "%s" in "%s"', $method, get_class($this)
78
                ));
79
            }
80
            $key = lcfirst(substr($method, 3));
81
            return $this->set($key, $args[0]);
82
        }
83
84
        throw new \BadMethodCallException(sprintf(
85
            'Unknown method "%s" in "%s"', $method, get_class($this)
86
        ));
87
    }
88
89
    /**
90
     * Clear all the images in this set.
91
     *
92
     * @return self
93
     */
94
    public function clear()
95
    {
96
        if ($this->images) {
97
            $this->images->clear();
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param Collection $images
105
     *
106
     * @return self
107
     */
108
    public function setImagesCollection(Collection $images)
109
    {
110
        $this->clear();
111
        $this->images = $images;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Set images and permissions.
118
     *
119
     * Replaces the whole set!
120
     *
121
     * @param array                $images
122
     * @param PermissionsInterface $permissions
0 ignored issues
show
Documentation introduced by
Should the type for parameter $permissions not be null|PermissionsInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
123
     *
124
     * @return self
125
     */
126
    public function setImages(array $images, PermissionsInterface $permissions = null)
127
    {
128
        $this->clear();
129
130
        foreach ($images as $prop => $image) {
131
            $this->set($prop, $image, /* check */ false);
132
        }
133
134
        if ($permissions) {
135
            $this->setPermissions($permissions);
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return ArrayCollection|Collection
143
     */
144
    protected function getImages()
145
    {
146
        if (!$this->images) {
147
            $this->images = new ArrayCollection();
148
        }
149
150
        return $this->images;
151
    }
152
153
    /**
154
     * Get an image
155
     *
156
     * If no image with the $key is found, the image with the key
157
     * self::ORIGINAL is returned. Unless self::ORIGINAL is requested
158
     * or $fallback is false - in that case NULL is returned.
159
     *
160
     * @param string $key
161
     * @param bool   $fallback true: Return ORIGINAL image, if not found.
162
     *
163
     * @return ImageInterface|null
164
     */
165
    public function get($key, $fallback = true)
166
    {
167
        foreach ($this->getImages() as $image) {
168
            /* @var ImageInterface $image */
169
            if ($key == $image->getKey()) {
170
                return $image;
171
            }
172
        }
173
174
        return !$fallback || self::ORIGINAL == $key ? null : $this->get(self::ORIGINAL);
175
    }
176
177
    /**
178
     * Set an image.
179
     *
180
     * Replaces any image with the same $key, unless $check is false.
181
     *
182
     * @param string         $key
183
     * @param ImageInterface $image
184
     * @param bool           $check
185
     *
186
     * @return self
187
     */
188
    public function set($key, ImageInterface $image, $check = true)
189
    {
190
        $images = $this->getImages();
191
        if ($check && ($img = $this->get($key))) {
192
            $images->removeElement($img);
193
        }
194
195
        $image->setBelongsTo($this->id);
196
        $image->setKey($key);
197
198
        $images->add($image);
199
200
        return $this;
201
    }
202
203
    /**
204
     * Set permissions for all images in this set.
205
     *
206
     * @param PermissionsInterface $permissions
207
     *
208
     * @return self
209
     */
210
    public function setPermissions(PermissionsInterface $permissions)
211
    {
212
        foreach ($this->getImages() as $file) {
213
            /* @var PermissionsInterface $filePermissions */
214
            /* @var \Core\Entity\FileInterface $file */
215
            $filePermissions = $file->getPermissions();
216
            $filePermissions->clear();
217
            $filePermissions->inherit($permissions);
218
        }
219
220
        return $this;
221
    }
222
}