Completed
Push — develop ( 725207...6c2664 )
by
unknown
16:33 queued 08:34
created

Cv::updatePermissions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Cv\Entity;
4
5
use Auth\Entity\InfoInterface;
6
use Auth\Entity\UserInterface;
7
use Core\Collection\IdentityWrapper;
8
use Core\Entity\AbstractIdentifiableEntity;
9
use Core\Entity\DraftableEntityInterface;
10
use Core\Entity\ModificationDateAwareEntityTrait;
11
use Core\Entity\PermissionsAwareTrait;
12
use Core\Entity\PermissionsInterface;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection as CollectionInterface;
15
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
16
use Zend\Permissions\Acl\Resource\ResourceInterface;
17
18
/**
19
 * Defines CV Model
20
 *
21
 * @ODM\Document(collection="cvs", repositoryClass="\Cv\Repository\Cv")
22
 * @ODM\Indexes({
23
 *     @ODM\Index(keys={
24
 *          "preferredJob.desiredJob"="text"
25
 *     },name="cvFulltext")
26
 * })
27
 * @ODM\HasLifecycleCallbacks
28
 */
29
class Cv extends AbstractIdentifiableEntity implements CvInterface, ResourceInterface
30
{
31
    use PermissionsAwareTrait, ModificationDateAwareEntityTrait;
32
33
    /**
34
     * Owner of the CV
35
     *
36
     * @var UserInterface
37
     * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true, cascade="persist")
38
     */
39
    protected $user;
40
    
41
    /**
42
     * personal informations, contains firstname, lastname, email,
43
     * phone etc.
44
     *
45
     * @ODM\EmbedOne(targetDocument="Contact")
46
     */
47
    protected $contact;
48
    
49
    /**
50
     * Education History
51
     *
52
     * @var ArrayCollection
53
     * @ODM\EmbedMany(targetDocument="\Cv\Entity\Education")
54
     */
55
    protected $educations;
56
    
57
    /**
58
     * Employment History
59
     *
60
     * @var ArrayCollection
61
     * @ODM\EmbedMany(targetDocument="\Cv\Entity\Employment")
62
     */
63
    protected $employments;
64
    
65
    /**
66
     * Skills
67
     *
68
     * @var ArrayCollection
69
     * @ODM\EmbedMany(targetDocument="\Cv\Entity\Skill")
70
     */
71
    protected $skills;
72
73
    /**
74
    * Skills
75
    *
76
    * @var ArrayCollection
77
    * @ODM\EmbedMany(targetDocument="\Cv\Entity\Language")
78
    */
79
    protected $languageSkills;
80
81
    /**
82
     * @var array
83
     * @ODM\Collection
84
     */
85
    protected $nativeLanguages=[];
86
87
88
    /**
89
     * Preferred Job. Where do the user want to work? What kind of work he wants do do
90
     *
91
     * @ODM\EmbedOne(targetDocument="\Cv\Entity\PreferredJob")
92
     */
93
    protected $preferredJob;
94
95
    /**
96
     * Flag indicating draft state of this cv.
97
     *
98
     * @var bool
99
     * @ODM\Boolean
100
     */
101
    protected $isDraft = false;
102
    
103
    /**
104
     * Status
105
     *
106
     * @var Status
107
     * @ODM\EmbedOne(targetDocument="Status")
108
     * @ODM\Index
109
     */
110
    protected $status;
111
    
112
    /**
113
     * Multiple attachments
114
     *
115
     * @since 0.26
116
     * @ODM\ReferenceMany(targetDocument="Attachment", simple="true", cascade={"persist", "remove"})
117
     */
118
    protected $attachments;
119
120
    public function __construct()
121
    {
122
        $this->status = new Status();
123
    }
124
    
125
    /**
126
     * @return UserInterface
127
     */
128
    public function getUser()
129
    {
130
        return $this->user;
131
    }
132
133
    /**
134
     * @param UserInterface $user
135
     * @return $this
136
     */
137
    public function setUser(UserInterface $user)
138
    {
139
        $oldUser    = $this->user;
140
        $this->user = $user;
141
        $this->updatePermissions($oldUser);
142
143
        return $this;
144
    }
145
146
    /**
147
     * Returns the string identifier of the Resource
148
     *
149
     * @return string
150
     */
151
    public function getResourceId()
152
    {
153
        return 'Entity/Cv';
154
    }
155
156
157
    /**
158
     * @return Contact
159
     */
160
    public function getContact()
161
    {
162
        return $this->contact;
163
    }
164
    
165
    /**
166
     * @return Cv
167
     */
168
    public function setContact(InfoInterface $contact)
169
    {
170
        if (!$contact instanceof Contact) {
171
            $contact = new Contact($contact);
172
        }
173
        $this->contact = $contact;
174
        return $this;
175
    }
176
    
177
    /**
178
     * @return ArrayCollection
179
     */
180
    public function getEducations()
181
    {
182
        if (!$this->educations) {
183
            $this->setEducations(new ArrayCollection());
184
        }
185
        return $this->educations;
186
    }
187
    
188
    /**
189
     * @return ArrayCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be IdentityWrapper?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
190
     */
191
    public function getEducationsIndexedById()
192
    {
193
        return new IdentityWrapper($this->getEducations());
194
    }
195
196
    /**
197
     * @param CollectionInterface $educations
198
     * @return $this
199
     */
200
    public function setEducations(CollectionInterface $educations)
201
    {
202
        $this->educations = $educations;
0 ignored issues
show
Documentation Bug introduced by
$educations is of type object<Doctrine\Common\Collections\Collection>, but the property $educations was declared to be of type object<Doctrine\Common\C...ctions\ArrayCollection>. 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...
203
        return $this;
204
    }
205
206
    /**
207
     * @return ArrayCollection
208
     */
209
    public function getEmployments()
210
    {
211
        if (!$this->employments) {
212
            $this->setEmployments(new ArrayCollection());
213
        }
214
        return $this->employments;
215
    }
216
    
217
    /**
218
     * @return ArrayCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be IdentityWrapper?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
219
     */
220
    public function getEmploymentsIndexedById()
221
    {
222
        return new IdentityWrapper($this->getEmployments());
223
    }
224
225
    /**
226
     * @param CollectionInterface $employments
227
     * @return $this
228
     */
229
    public function setEmployments(CollectionInterface $employments)
230
    {
231
        $this->employments = $employments;
0 ignored issues
show
Documentation Bug introduced by
$employments is of type object<Doctrine\Common\Collections\Collection>, but the property $employments was declared to be of type object<Doctrine\Common\C...ctions\ArrayCollection>. 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...
232
        return $this;
233
    }
234
    
235
    /**
236
     * @return ArrayCollection
237
     */
238
    public function getSkills()
239
    {
240
        if (!$this->skills) {
241
            $this->setSkills(new ArrayCollection());
242
        }
243
        return $this->skills;
244
    }
245
    
246
    /**
247
     * @return ArrayCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be IdentityWrapper?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
248
     */
249
    public function getSkillsIndexedById()
250
    {
251
        return new IdentityWrapper($this->getSkills());
252
    }
253
254
    /**
255
     * @param CollectionInterface $skills
256
     * @return $this
257
     */
258
    public function setSkills(CollectionInterface $skills)
259
    {
260
        $this->skills = $skills;
0 ignored issues
show
Documentation Bug introduced by
$skills is of type object<Doctrine\Common\Collections\Collection>, but the property $skills was declared to be of type object<Doctrine\Common\C...ctions\ArrayCollection>. 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...
261
        return $this;
262
    }
263
264
    /**
265
     * @param bool $isDraft
266
     * @return $this
267
     */
268
    public function setIsDraft($isDraft)
269
    {
270
        $this->isDraft=$isDraft;
271
        return $this;
272
    }
273
274
    /**
275
     * @return boolean
276
     */
277
    public function isDraft()
278
    {
279
        return $this->isDraft;
280
    }
281
282
    /**
283
     * @return \Cv\Entity\PreferredJobInterface
284
     */
285
    public function getPreferredJob()
286
    {
287
        if (null == $this->preferredJob) {
288
            $this->preferredJob = new PreferredJob();
289
        }
290
        return $this->preferredJob;
291
    }
292
293
    /**
294
     * @param \Cv\Entity\PreferredJobInterface $preferredJob
295
     * @return $this
296
     */
297
    public function setPreferredJob(PreferredJobInterface $preferredJob)
298
    {
299
        $this->preferredJob = $preferredJob;
300
        return $this;
301
    }
302
303
    /**
304
     * @return ArrayCollection
305
     */
306
    public function getLanguageSkills()
307
    {
308
        if (!$this->languageSkills) {
309
            $this->setLanguageSkills(new ArrayCollection());
310
        }
311
        return $this->languageSkills;
312
    }
313
314
    /**
315
     * @param CollectionInterface $languageSkills
316
     * @return $this
317
     */
318
    public function setLanguageSkills(CollectionInterface $languageSkills)
319
    {
320
        $this->languageSkills = $languageSkills;
0 ignored issues
show
Documentation Bug introduced by
$languageSkills is of type object<Doctrine\Common\Collections\Collection>, but the property $languageSkills was declared to be of type object<Doctrine\Common\C...ctions\ArrayCollection>. 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...
321
        return $this;
322
    }
323
324
    /**
325
     * @return ArrayCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be IdentityWrapper?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
326
     */
327
    public function getLanguageSkillsIndexedById()
328
    {
329
        return new IdentityWrapper($this->getLanguageSkills());
330
    }
331
332
    /**
333
     * Sets the mothers tongue of the candidate
334
     *
335
     * @param array
336
     * @return $this
337
     */
338
    public function setNativeLanguages($nativeLanguages)
339
    {
340
        $this->nativeLanguages=$nativeLanguages;
341
        return $this;
342
    }
343
344
    /**
345
     * Gets the mothers tongue of the candidate
346
     *
347
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
348
     */
349
    public function getNativeLanguages()
350
    {
351
        return $this->nativeLanguages;
352
    }
353
    
354
    /**
355
     * @return Status
356
     */
357
    public function getStatus()
358
    {
359
        return $this->status;
360
    }
361
    
362
    /**
363
     * @param Status|string $status
364
     */
365
    public function setStatus($status)
366
    {
367
        if (!$status instanceof Status) {
368
            $status = new Status($status);
369
        }
370
    
371
        $this->status = $status;
372
373
        return $this;
374
    }
375
376
    /**
377
     * @param CollectionInterface $attachments
378
     * @return Cv
379
     * @since 0.26
380
     */
381
    public function setAttachments(CollectionInterface $attachments)
382
    {
383
        $this->attachments = $attachments;
384
        return $this;
385
    }
386
387
    /**
388
     * @return CollectionInterface
389
     * @since 0.26
390
     */
391
    public function getAttachments()
392
    {
393
        if (!$this->attachments) {
394
            $this->setAttachments(new ArrayCollection());
395
        }
396
        return $this->attachments;
397
    }
398
399
    /**
400
     *
401
     * @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...
402
     */
403
    private function setupPermissions(PermissionsInterface $permissions = null)
404
    {
405
        if ($this->user) {
406
            $permissions->grant($this->user, PermissionsInterface::PERMISSION_ALL);
0 ignored issues
show
Bug introduced by
It seems like $permissions is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
407
        }
408
    }
409
410
    private function updatePermissions($oldUser = null)
411
    {
412
        $hasPermissions = (bool) $this->permissions;
413
        $permissions = $this->getPermissions();
414
415
        if ($hasPermissions) {
416
            $oldUser && $permissions->revoke($oldUser, PermissionsInterface::PERMISSION_ALL);
417
            $this->setupPermissions($permissions);
418
        }
419
420
        /*
421
         * getPermissions() already granted the user we need not to do anything.
422
         */
423
    }
424
}
425