Completed
Pull Request — master (#139)
by olivier
11:37
created

Badge::getImagePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Entity;
4
5
use Badger\Component\Game\Model\BadgeInterface;
6
use Badger\Component\Tag\Model\TagInterface;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use JsonSerializable;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
/**
12
 * Badge entity.
13
 *
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class Badge implements BadgeInterface, JsonSerializable
17
{
18
    /** @var string */
19
    protected $file;
20
21
    /** @var string */
22
    protected $id;
23
24
    /** @var string */
25
    protected $title;
26
27
    /** @var string */
28
    protected $description;
29
30
    /** @var string */
31
    protected $imagePath;
32
33
    /** @var ArrayCollection */
34
    protected $tags;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getId()
40
    {
41
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getTitle()
48
    {
49
        return $this->title;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function setTitle($title)
56
    {
57
        $this->title = $title;
58
59
        return $this;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getDescription()
66
    {
67
        return $this->description;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function setDescription($description)
74
    {
75
        $this->description = $description;
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getImagePath()
84
    {
85
        return $this->imagePath;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function setImagePath($imagePath)
92
    {
93
        $this->imagePath = $imagePath;
94
95
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function setFile(UploadedFile $file = null)
102
    {
103
        $this->file = $file;
0 ignored issues
show
Documentation Bug introduced by
It seems like $file can also be of type object<Symfony\Component...tion\File\UploadedFile>. However, the property $file is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getFile()
110
    {
111
        return $this->file;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->file; (string) is incompatible with the return type declared by the interface Badger\Component\Game\Mo...BadgeInterface::getFile of type Symfony\Component\HttpFoundation\File\UploadedFile.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function upload()
118
    {
119
        // the file property can be empty if the field is not required
120
        if (null === $this->getFile()) {
121
            return;
122
        }
123
124
        // use the original file name here but you should
125
        // sanitize it at least to avoid any security issues
126
127
        // move takes the target directory and then the
128
        // target filename to move to
129
        $this->getFile()->move(
0 ignored issues
show
Bug introduced by
The method move cannot be called on $this->getFile() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
130
            $this->getUploadRootDir(),
131
            $this->getFile()->getClientOriginalName()
0 ignored issues
show
Bug introduced by
The method getClientOriginalName cannot be called on $this->getFile() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
132
        );
133
134
        // set the path property to the filename where you've saved the file
135
        $this->imagePath = $this->getFile()->getClientOriginalName();
0 ignored issues
show
Bug introduced by
The method getClientOriginalName cannot be called on $this->getFile() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
136
137
        // clean up the file property as you won't need it anymore
138
        $this->file = null;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function addTag(TagInterface $tag)
145
    {
146
        $this->tags[] = $tag;
147
148
        return $this;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function setTags(ArrayCollection $tags)
155
    {
156
        $this->tags = $tags;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getTags()
163
    {
164
        return $this->tags;
165
    }
166
167
    /**
168
     * Returns the absolute path of the image, null if no image
169
     *
170
     * @return null|string
171
     */
172
    public function getImageAbsolutePath()
173
    {
174
        return null === $this->imagePath
175
            ? null
176
            : $this->getUploadRootDir() . '/' . $this->imagePath;
177
    }
178
179
    /**
180
     * Returns the path of the image for the web, null if no image
181
     *
182
     * @return null|string
183
     */
184
    public function getImageWebPath()
185
    {
186
        return null === $this->imagePath
187
            ? null
188
            : '/' . $this->getUploadDir() . '/' . $this->imagePath;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function jsonSerialize()
195
    {
196
        return [
197
            'id'           => $this->id,
198
            'title'        => $this->title,
199
            'description'  => $this->description,
200
            'imageWebPath' => $this->getImageWebPath(),
201
        ];
202
    }
203
204
    /**
205
     * Returns the absolute directory path where uploaded documents should be saved
206
     *
207
     * @return string
208
     */
209
    protected function getUploadRootDir()
210
    {
211
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
212
    }
213
214
    /**
215
     * Returns the path where upload files.
216
     *
217
     * Get rid of the __DIR__ so it doesn't screw up when displaying uploaded doc/image in the view.
218
     *
219
     * @return string
220
     */
221
    protected function getUploadDir()
222
    {
223
        return 'uploads/game';
224
    }
225
226
    /**
227
     * @return string
228
     */
229
    public function __toString()
230
    {
231
        return $this->title;
232
    }
233
}
234