Completed
Push — master ( bceb2e...41b022 )
by Quentin
05:56
created

Image::setDefaultFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Synapse\Cmf\Framework\Media\Image\Entity;
4
5
use Majora\Framework\Normalizer\Model\NormalizableTrait;
6
use Synapse\Cmf\Framework\Media\Format\Model\FormatInterface;
7
use Synapse\Cmf\Framework\Media\FormattedImage\Entity\FormattedImageCollection;
8
use Synapse\Cmf\Framework\Media\FormattedImage\Model\FormattedImageInterface;
9
use Synapse\Cmf\Framework\Media\Image\Model\ImageInterface;
10
use Synapse\Cmf\Framework\Media\Media\Entity\Media;
11
12
/**
13
 * Image entity class.
14
 */
15
class Image extends Media implements ImageInterface
16
{
17
    use NormalizableTrait;
18
19
    /**
20
     * @var int
21
     */
22
    protected $id;
23
24
    /**
25
     * Override and immutable type, image are always type "image".
26
     *
27
     * @var string
28
     */
29
    protected $type = 'image';
30
31
    /**
32
     * @var string
33
     */
34
    protected $defaultFormat;
35
36
    /**
37
     * @var array
38
     */
39
    protected $tags;
40
41
    /**
42
     * @var string
43
     */
44
    protected $headline;
45
46
    /**
47
     * @var string
48
     */
49
    protected $alt;
50
51
    /**
52
     * @var FormattedImageCollection
53
     */
54
    protected $formattedImages;
55
56
    /**
57
     * Construct.
58
     */
59
    public function __construct()
60
    {
61
        $this->tags = array();
62
        $this->formattedImages = new FormattedImageCollection();
63
    }
64
65
    /**
66
     * @see NormalizableInterface::getScopes()
67
     */
68
    public static function getScopes()
69
    {
70
        return array(
71
            'id' => 'id',
72
            'default' => array('id', 'type'),
73
        );
74
    }
75
76
    /**
77
     * Returns Image id.
78
     *
79
     * @return int
80
     */
81
    public function getId()
82
    {
83
        return $this->id;
84
    }
85
86
    /**
87
     * Define Image id.
88
     *
89
     * @param int $id
90
     *
91
     * @return self
92
     */
93
    public function setId($id)
94
    {
95
        $this->id = $id;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Returns image type.
102
     *
103
     * @return string
104
     */
105
    public function getType()
106
    {
107
        return $this->type;
108
    }
109
110
    /**
111
     * Returns image tags.
112
     *
113
     * @return array
114
     */
115
    public function getTags()
116
    {
117
        return $this->tags;
118
    }
119
120
    /**
121
     * Define image tags.
122
     *
123
     * @param array $tags
124
     *
125
     * @return self
126
     */
127
    public function setTags(array $tags)
128
    {
129
        $this->tags = $tags;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Returns image headline.
136
     *
137
     * @return string
138
     */
139
    public function getHeadline()
140
    {
141
        return $this->headline;
142
    }
143
144
    /**
145
     * Define image headline.
146
     *
147
     * @param string $headline
148
     *
149
     * @return self
150
     */
151
    public function setHeadline($headline)
152
    {
153
        $this->headline = $headline;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Returns image alt.
160
     *
161
     * @return string
162
     */
163
    public function getAlt()
164
    {
165
        return $this->alt;
166
    }
167
168
    /**
169
     * Define image alt.
170
     *
171
     * @param string $alt
172
     *
173
     * @return self
174
     */
175
    public function setAlt($alt)
176
    {
177
        $this->alt = $alt;
178
179
        return $this;
180
    }
181
182
    /**
183
     * Returns image formatted images.
184
     *
185
     * @return FormattedImageCollection
186
     */
187
    public function getFormattedImages()
188
    {
189
        return $this->formattedImages;
190
    }
191
192
    /**
193
     * Define image formatted images.
194
     *
195
     * @param FormattedImageCollection $formattedImages
196
     *
197
     * @return self
198
     */
199
    public function setFormattedImages(FormattedImageCollection $formattedImages)
200
    {
201
        $this->formattedImages = $formattedImages;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Returns image formatted for given format name or FormatInterface object.
208
     *
209
     * @param string|FormatInterface $format
210
     *
211
     * @return FormattedImageInterface|null
212
     */
213
    public function getFormattedImage($format)
214
    {
215
        $formatName = $format instanceof FormatInterface
216
            ? $format->getName()
217
            : $format
218
        ;
219
220
        return $this->formattedImages
221
            ->filter(function (FormattedImageInterface $formattedImage) use ($formatName) {
222
                return ($format = $formattedImage->getFormat())
223
                    && $format->getName() == $formatName
224
                ;
225
            })
226
            ->first() ?: null // first() returns "false" if collection empty
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
227
        ;
228
    }
229
230
    /**
231
     * Returns requested format web path, or original one, if format doesnt exists.
232
     *
233
     * @param string|FormatInterface $format
234
     *
235
     * @return string
236
     */
237
    public function getFormatWebPath($format = null)
238
    {
239
        if (!$format = $format ?: $this->defaultFormat) {
240
            return $this->getWebPath();
241
        }
242
243
        return ($formattedImage = $this->getFormattedImage($format))
244
            ? $formattedImage->getWebPath()
245
            : $this->getWebPath()
246
        ;
247
    }
248
249
    /**
250
     * Define object default format.
251
     *
252
     * @param string $defaultFormat
253
     *
254
     * @return self
255
     */
256
    public function setDefaultFormat($defaultFormat)
257
    {
258
        $this->defaultFormat = $defaultFormat;
259
260
        return $this;
261
    }
262
}
263