Completed
Pull Request — master (#70)
by
unknown
02:02
created

ImagineBlock::setImage()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 17
nc 6
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Symfony CMF package.
5
 *
6
 * (c) 2011-2017 Symfony CMF
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lakion\CmsPlugin\Document;
13
14
use PHPCR\NodeInterface;
15
use Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\AbstractBlock;
16
use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image;
17
use Symfony\Cmf\Bundle\MediaBundle\ImageInterface;
18
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
19
use Symfony\Component\HttpFoundation\File\UploadedFile;
20
21
/**
22
 * Block to hold an image.
23
 */
24
class ImagineBlock extends AbstractBlock implements TranslatableInterface
25
{
26
    /**
27
     * @var Image
28
     */
29
    protected $image;
30
31
    /**
32
     * @var string
33
     */
34
    protected $label;
35
36
    /**
37
     * Optional link url to use on the image.
38
     *
39
     * @var string
40
     */
41
    protected $linkUrl;
42
43
    /**
44
     * @var string
45
     */
46
    protected $filter;
47
48
    /**
49
     * @var \PHPCR\NodeInterface
50
     */
51
    protected $node;
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getType()
57
    {
58
        return 'cmf.block.imagine';
59
    }
60
61
    /**
62
     * Set label.
63
     *
64
     * @param string $label
65
     *
66
     * @return $this
67
     */
68
    public function setLabel($label)
69
    {
70
        $this->label = $label;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Get label.
77
     *
78
     * @return string
79
     */
80
    public function getLabel()
81
    {
82
        return $this->label;
83
    }
84
85
    /**
86
     * Set link url.
87
     *
88
     * @param string $url
89
     *
90
     * @return $this
91
     */
92
    public function setLinkUrl($url)
93
    {
94
        $this->linkUrl = $url;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Get link url.
101
     *
102
     * @return string
103
     */
104
    public function getLinkUrl()
105
    {
106
        return $this->linkUrl;
107
    }
108
109
    /**
110
     * Sets the Imagine filter which is going to be used.
111
     *
112
     * @param string $filter
113
     *
114
     * @return $this
115
     */
116
    public function setFilter($filter)
117
    {
118
        $this->filter = $filter;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Get the Imagine filter.
125
     *
126
     * @return string
127
     */
128
    public function getFilter()
129
    {
130
        return $this->filter;
131
    }
132
133
    /**
134
     * Set the image for this block.
135
     *
136
     * Setting null will do nothing, as this is what happens when you edit this
137
     * block in a form without uploading a replacement file.
138
     *
139
     * If you need to delete the Image, you can use getImage and delete it with
140
     * the document manager. Note that this block does not make much sense
141
     * without an image, though.
142
     *
143
     * @param ImageInterface|UploadedFile|null $image optional the image to update
144
     *
145
     * @return $this
146
     *
147
     * @throws \InvalidArgumentException if the $image parameter can not be handled
148
     */
149
    public function setImage($image = null)
150
    {
151
        if (!$image) {
152
            return $this;
153
        }
154
155
        if (!$image instanceof ImageInterface && !$image instanceof UploadedFile) {
156
            $type = is_object($image) ? get_class($image) : gettype($image);
157
158
            throw new \InvalidArgumentException(sprintf(
159
                'Image is not a valid type, "%s" given.',
160
                $type
161
            ));
162
        }
163
164
        if ($this->image) {
165
            // existing image, only update content
166
            // TODO: https://github.com/doctrine/phpcr-odm/pull/262
167
            $this->image->copyContentFromFile($image);
168
        } elseif ($image instanceof ImageInterface) {
169
            $image->setName('image'); // ensure document has right name
170
            $this->image = $image;
0 ignored issues
show
Documentation Bug introduced by
$image is of type object<Symfony\Cmf\Bundl...aBundle\ImageInterface>, but the property $image was declared to be of type object<Symfony\Cmf\Bundl...e\Doctrine\Phpcr\Image>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
171
        } else {
172
            $this->image = new Image();
173
            $this->image->copyContentFromFile($image);
174
        }
175
176
        return $this;
177
    }
178
179
    /**
180
     * Get image.
181
     *
182
     * @return Image
183
     */
184
    public function getImage()
185
    {
186
        return $this->image;
187
    }
188
189
    /**
190
     * Get node.
191
     *
192
     * @return NodeInterface
193
     */
194
    public function getNode()
195
    {
196
        return $this->node;
197
    }
198
}
199