1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Badger\Bundle\GameBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Badger\Component\Game\Model\BadgeInterface; |
6
|
|
|
use Badger\Component\Game\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
|
|
|
/** @var ArrayCollection */ |
37
|
|
|
protected $completions; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function getId() |
43
|
|
|
{ |
44
|
|
|
return $this->id; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getTitle() |
51
|
|
|
{ |
52
|
|
|
return $this->title; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function setTitle($title) |
59
|
|
|
{ |
60
|
|
|
$this->title = $title; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function getDescription() |
69
|
|
|
{ |
70
|
|
|
return $this->description; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function setDescription($description) |
77
|
|
|
{ |
78
|
|
|
$this->description = $description; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getImagePath() |
87
|
|
|
{ |
88
|
|
|
return $this->imagePath; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function setImagePath($imagePath) |
95
|
|
|
{ |
96
|
|
|
$this->imagePath = $imagePath; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function setFile(UploadedFile $file = null) |
105
|
|
|
{ |
106
|
|
|
$this->file = $file; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getFile() |
113
|
|
|
{ |
114
|
|
|
return $this->file; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function upload() |
121
|
|
|
{ |
122
|
|
|
// the file property can be empty if the field is not required |
123
|
|
|
if (null === $this->getFile()) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// use the original file name here but you should |
128
|
|
|
// sanitize it at least to avoid any security issues |
129
|
|
|
|
130
|
|
|
// move takes the target directory and then the |
131
|
|
|
// target filename to move to |
132
|
|
|
$this->getFile()->move( |
|
|
|
|
133
|
|
|
$this->getUploadRootDir(), |
134
|
|
|
$this->getFile()->getClientOriginalName() |
|
|
|
|
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
// set the path property to the filename where you've saved the file |
138
|
|
|
$this->imagePath = $this->getFile()->getClientOriginalName(); |
|
|
|
|
139
|
|
|
|
140
|
|
|
// clean up the file property as you won't need it anymore |
141
|
|
|
$this->file = null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function addTag(TagInterface $tag) |
148
|
|
|
{ |
149
|
|
|
$this->tags[] = $tag; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function setTags(ArrayCollection $tags) |
158
|
|
|
{ |
159
|
|
|
$this->tags = $tags; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getTags() |
166
|
|
|
{ |
167
|
|
|
return $this->tags; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function getCompletions() |
174
|
|
|
{ |
175
|
|
|
return $this->completions; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function setCompletions($completions) |
182
|
|
|
{ |
183
|
|
|
$this->completions = $completions; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns the absolute path of the image, null if no image |
188
|
|
|
* |
189
|
|
|
* @return null|string |
190
|
|
|
*/ |
191
|
|
|
public function getImageAbsolutePath() |
192
|
|
|
{ |
193
|
|
|
return null === $this->imagePath |
194
|
|
|
? null |
195
|
|
|
: $this->getUploadRootDir() . '/' . $this->imagePath; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns the path of the image for the web, null if no image |
200
|
|
|
* |
201
|
|
|
* @return null|string |
202
|
|
|
*/ |
203
|
|
|
public function getImageWebPath() |
204
|
|
|
{ |
205
|
|
|
return null === $this->imagePath |
206
|
|
|
? null |
207
|
|
|
: '/' . $this->getUploadDir() . '/' . $this->imagePath; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
|
|
public function jsonSerialize() |
214
|
|
|
{ |
215
|
|
|
return [ |
216
|
|
|
'id' => $this->id, |
217
|
|
|
'title' => $this->title, |
218
|
|
|
'description' => $this->description, |
219
|
|
|
'imageWebPath' => $this->getImageWebPath(), |
220
|
|
|
]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Returns the absolute directory path where uploaded documents should be saved |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
protected function getUploadRootDir() |
229
|
|
|
{ |
230
|
|
|
return __DIR__ . '/../../../../../web/' . $this->getUploadDir(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns the path where upload files. |
235
|
|
|
* |
236
|
|
|
* Get rid of the __DIR__ so it doesn't screw up when displaying uploaded doc/image in the view. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
protected function getUploadDir() |
241
|
|
|
{ |
242
|
|
|
return 'uploads/game'; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public function __toString() |
249
|
|
|
{ |
250
|
|
|
return $this->title; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
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 theid
property of an instance of theAccount
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.