Completed
Push — issue#666 ( 6be2d0...f5ce0d )
by Guilherme
03:34
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OAuthBundle\Entity;
12
13
use LoginCidadao\CoreBundle\Entity\Authorization;
14
use FOS\OAuthServerBundle\Entity\Client as BaseClient;
15
use Doctrine\ORM\Mapping as ORM;
16
use LoginCidadao\CoreBundle\Model\PersonInterface;
17
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
18
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface;
19
use Symfony\Component\HttpFoundation\File\File;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
22
use JMS\Serializer\Annotation as JMS;
23
use OAuth2\OAuth2;
24
use LoginCidadao\OAuthBundle\Model\ClientInterface;
25
use Vich\UploaderBundle\Mapping\Annotation as Vich;
26
27
/**
28
 * @ORM\Entity(repositoryClass="LoginCidadao\OAuthBundle\Entity\ClientRepository")
29
 * @ORM\Table(name="client")
30
 * @ORM\HasLifecycleCallbacks
31
 * @UniqueEntity("name")
32
 * @JMS\ExclusionPolicy("all")
33
 * @Vich\Uploadable
34
 */
35
class Client extends BaseClient implements ClientInterface, ClaimProviderInterface
36
{
37
    /**
38
     * @ORM\Id
39
     * @ORM\Column(type="integer")
40
     * @ORM\GeneratedValue(strategy="AUTO")
41
     * @JMS\Expose
42
     * @JMS\Groups({"public"})
43
     */
44
    protected $id;
45
46
    /**
47
     * @ORM\Column(type="string", nullable=false, unique=true)
48
     * @JMS\Expose
49
     * @JMS\SerializedName("client_name")
50
     * @JMS\Groups({"public", "remote_claim"})
51
     */
52
    protected $name;
53
54
    /**
55
     * @ORM\Column(type="string", length=4000, nullable=true)
56
     * @JMS\Expose
57
     * @JMS\Groups({"public"})
58
     */
59
    protected $description;
60
61
    /**
62
     * @ORM\Column(type="string", length=2000, nullable=true)
63
     * @JMS\Expose
64
     * @JMS\Groups({"public"})
65
     */
66
    protected $landingPageUrl;
67
68
    /**
69
     * @ORM\Column(type="string", length=2000, nullable=true)
70
     * @JMS\Expose
71
     * @JMS\Groups({"public"})
72
     */
73
    protected $termsOfUseUrl;
74
75
    /**
76
     * @ORM\Column(type="json_array", nullable=false)
77
     */
78
    protected $allowedScopes;
79
80
    /**
81
     * @ORM\OneToMany(targetEntity="LoginCidadao\CoreBundle\Entity\Authorization", mappedBy="client", cascade={"remove"}, orphanRemoval=true)
82
     * @var Authorization[]
83
     */
84
    protected $authorizations;
85
86
    /**
87
     * @ORM\Column(type="string", length=2000, nullable=true)
88
     * @JMS\Expose
89
     * @JMS\Groups({"public"})
90
     */
91
    protected $siteUrl;
92
93
    /**
94
     * @Assert\File(
95
     *      maxSize="2M",
96
     *      maxSizeMessage="The maxmimum allowed file size is 2MB.",
97
     *      mimeTypes={"image/png", "image/jpeg", "image/pjpeg"},
98
     *      mimeTypesMessage="Only JPEG and PNG images are allowed."
99
     * )
100
     * @Vich\UploadableField(mapping="client_image", fileNameProperty="imageName")
101
     * @var File $image
102
     * @JMS\Since("1.0.2")
103
     */
104
    protected $image;
105
106
    /**
107
     * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
108
     *
109
     * @var string $imageName
110
     * @JMS\Since("1.0.2")
111
     */
112
    protected $imageName;
113
114
    /**
115
     * @ORM\Column(type="boolean", nullable=false)
116
     * @JMS\Expose
117
     * @JMS\Groups({"public"})
118
     */
119
    protected $published;
120
121
    /**
122
     * @ORM\Column(type="boolean", nullable=false)
123
     * @JMS\Expose
124
     * @JMS\Groups({"public"})
125
     */
126
    protected $visible;
127
128
    /**
129
     * @ORM\ManyToMany(targetEntity="LoginCidadao\CoreBundle\Entity\Person", inversedBy="clients"  )
130
     * @ORM\JoinTable(name="client_owners",
131
     *      joinColumns={@ORM\JoinColumn(name="person_id", referencedColumnName="id")},
132
     *      inverseJoinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")}
133
     *      )
134
     * @var PersonInterface[]
135
     */
136
    protected $owners;
137
138
    /**
139
     * @ORM\OneToMany(targetEntity="LoginCidadao\APIBundle\Entity\LogoutKey", mappedBy="client")
140
     */
141
    protected $logoutKeys;
142
143
    /**
144
     * @var \LoginCidadao\OpenIDBundle\Entity\ClientMetadata
145
     * @ORM\OneToOne(targetEntity="LoginCidadao\OpenIDBundle\Entity\ClientMetadata", mappedBy="client", cascade={"persist"})
146
     */
147
    protected $metadata;
148
149
    /**
150
     * @ORM\Column(name="updated_at", type="datetime")
151
     */
152
    protected $updatedAt;
153
    /**
154
     * @ORM\Column(type="string", nullable=true, unique=true)
155
     * @var string
156
     */
157
    private $uid;
158 35
159
    public function __construct()
160 35
    {
161 35
        parent::__construct();
162 35
        $this->authorizations = [];
163
        $this->owners = [];
164 35
165
        $this->allowedScopes = array(
166
            'public_profile',
167
            'openid',
168 35
        );
169
    }
170 1
171
    public static function getAllGrants()
172
    {
173 1
        return array(
174
            OAuth2::GRANT_TYPE_AUTH_CODE,
175
            OAuth2::GRANT_TYPE_IMPLICIT,
176
            OAuth2::GRANT_TYPE_USER_CREDENTIALS,
177
            OAuth2::GRANT_TYPE_CLIENT_CREDENTIALS,
178
            OAuth2::GRANT_TYPE_REFRESH_TOKEN,
179
            OAuth2::GRANT_TYPE_EXTENSIONS,
180
        );
181
    }
182 8
183
    /**
184 8
     * @return string
185 2
     */
186 2
    public function getName()
187
    {
188 1
        if ($this->getMetadata()) {
189
            if ($this->getMetadata()->getClientName() === null &&
190
                $this->name !== null
191 2
            ) {
192
                $this->getMetadata()->setClientName($this->name);
193
            }
194 7
195
            return $this->getMetadata()->getClientName();
196
        }
197 14
198
        return $this->name;
199 14
    }
200 1
201
    public function setName($name)
202 14
    {
203
        if ($this->getMetadata()) {
204 14
            $this->getMetadata()->setClientName($name);
205
        }
206
        $this->name = $name;
207 2
208
        return $this;
209 2
    }
210
211
    public function getDescription()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
212 2
    {
213
        return $this->description;
214 2
    }
215
216 2
    public function setDescription($description)
217
    {
218
        $this->description = $description;
219 3
220
        return $this;
221 3
    }
222 1
223
    public function getSiteUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
224
    {
225 3
        if ($this->getMetadata()) {
226
            return $this->getMetadata()->getClientUri();
227
        }
228 3
229
        return $this->siteUrl;
230 3
    }
231 1
232
    public function setSiteUrl($url)
233 3
    {
234
        if ($this->getMetadata()) {
235 3
            $this->getMetadata()->setClientUri($url);
236
        }
237
        $this->siteUrl = $url;
238
239
        return $this;
240
    }
241
242 2
    /**
243
     * @param array|Authorization[] $authorizations
244 2
     * @return $this
245
     */
246 2
    public function setAuthorizations($authorizations = [])
247
    {
248
        $this->authorizations = $authorizations;
249
250
        return $this;
251
    }
252 2
253
    /**
254 2
     * @return array|Authorization[]
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Authorization[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
255
     */
256
    public function getAuthorizations()
257
    {
258
        return $this->authorizations;
259
    }
260 2
261
    /**
262 2
     * @param Authorization $authorization
263 2
     */
264 2
    public function removeAuthorization(Authorization $authorization)
265
    {
266
        foreach ($this->authorizations as $k => $candidate) {
267 2
            if ($candidate->getId() === $authorization->getId()) {
268
                unset($this->authorizations[$k]);
269 2
            }
270
        }
271 2
    }
272 1
273
    public function getLandingPageUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
274
    {
275 2
        if ($this->getMetadata()) {
276
            return $this->getMetadata()->getInitiateLoginUri();
277
        }
278 2
279
        return $this->landingPageUrl;
280 2
    }
281 1
282
    public function setLandingPageUrl($landingPageUrl)
283 2
    {
284
        if ($this->getMetadata()) {
285 2
            $this->getMetadata()->setInitiateLoginUri($landingPageUrl);
286
        }
287
        $this->landingPageUrl = $landingPageUrl;
288 3
289
        return $this;
290 3
    }
291 1
292
    public function getTermsOfUseUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
293
    {
294 3
        if ($this->getMetadata()) {
295
            return $this->getMetadata()->getTosUri();
296
        }
297 3
298
        return $this->termsOfUseUrl;
299 3
    }
300 1
301
    public function setTermsOfUseUrl($termsOfUseUrl)
302 3
    {
303
        if ($this->getMetadata()) {
304 3
            $this->getMetadata()->setTosUri($termsOfUseUrl);
305
        }
306
        $this->termsOfUseUrl = $termsOfUseUrl;
307 2
308
        return $this;
309 2
    }
310
311 2
    public function getAllowedScopes()
312 2
    {
313
        $scopes = ['public_profile', 'openid'];
314
315 2
        if (is_array($this->allowedScopes)) {
316
            $scopes = $this->allowedScopes;
317
        }
318 7
319
        return $scopes;
320 7
    }
321
322 7
    public function setAllowedScopes(array $allowedScopes)
323
    {
324
        $this->allowedScopes = $allowedScopes;
325 1
326
        return $this;
327 1
    }
328
329
    public function isVisible()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
330 2
    {
331
        return $this->visible;
332 2
    }
333
334 2
    public function setVisible($visible)
335
    {
336
        $this->visible = $visible;
337 1
338
        return $this;
339 1
    }
340
341
    public function isPublished()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
342 2
    {
343
        return $this->published;
344 2
    }
345
346 2
    public function setPublished($published)
347
    {
348
        $this->published = $published;
349 7
350
        return $this;
351 7
    }
352
353 7
    public function setId($var)
354
    {
355
        $this->id = $var;
356 2
357
        return $this;
358 2
    }
359
360
    public function getOwners()
361
    {
362
        return $this->owners;
363 2
    }
364
365 2
    /* Unique Interface Stuff */
366
367 2
    public function setOwners($owners)
368
    {
369
        $this->owners = $owners;
370
371
        return $this;
372
    }
373
374 1
    /**
375
     * Gets the Unique Id of the Entity.
376 1
     * @return string the entity UID
377
     */
378
    public function getUid()
379
    {
380
        return $this->uid;
381
    }
382
383
    /**
384 1
     * Sets the Unique Id of the Entity.
385
     * @param string $uid the entity UID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $uid not be string|null?

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...
386 1
     * @return $this
387
     */
388 1
    public function setUid($uid = null)
389
    {
390
        $this->uid = $uid;
391
392
        return $this;
393
    }
394 1
395
    /**
396 1
     * Compatibility with OIDC code
397
     */
398
    public function getClientId()
399
    {
400
        return $this->getPublicId();
401
    }
402 4
403
    /**
404 4
     * Compatibility with OIDC code
405
     */
406
    public function getClientSecret()
407 21
    {
408
        return $this->getSecret();
409 21
    }
410
411
    public function getMetadata()
412 3
    {
413
        return $this->metadata;
414 3
    }
415
416 3
    public function setMetadata(ClientMetadata $metadata)
417
    {
418
        $this->metadata = $metadata;
419 11
420
        return $this;
421 11
    }
422 1
423
    /**
424
     * @JMS\VirtualProperty()
425 11
     * @JMS\SerializedName("redirect_uris")
426
     * @JMS\Groups({"remote_claim"})
427
     * @return array|string[]
428 19
     */
429
    public function getRedirectUris()
430 19
    {
431 1
        if ($this->getMetadata()) {
432
            return $this->getMetadata()->getRedirectUris();
433 19
        }
434
435
        return parent::getRedirectUris();
436 19
    }
437
438
    public function setRedirectUris(array $redirectUris)
439
    {
440
        if ($this->getMetadata()) {
441
            $this->getMetadata()->setRedirectUris($redirectUris);
442 2
        } else {
443
            parent::setRedirectUris($redirectUris);
444 2
        }
445
446
        return $this;
447
    }
448
449
    /**
450
     * @return File
451
     */
452
    public function getImage()
453
    {
454
        return $this->image;
455
    }
456
457 1
    /**
458
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
459 1
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
460
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
461 1
     * must be able to accept an instance of 'File' as the bundle will inject one here
462 1
     * during Doctrine hydration.
463
     *
464
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
465 1
     * @return $this
466
     */
467
    public function setImage($image)
468
    {
469
        $this->image = $image;
470
471 1
        if ($this->image) {
472
            $this->updatedAt = new \DateTime('now');
473 1
        }
474
475
        return $this;
476
    }
477
478
    /**
479
     * @return string
480 1
     */
481
    public function getImageName()
482 1
    {
483
        return $this->imageName;
484 1
    }
485
486
    /**
487 1
     * @param string $imageName
488
     * @return $this
489 1
     */
490
    public function setImageName($imageName)
491
    {
492
        $this->imageName = $imageName;
493
494
        return $this;
495
    }
496
497
    public function getUpdatedAt()
498 2
    {
499
        return $this->updatedAt;
500 2
    }
501 1
502
    /**
503 2
     * @ORM\PrePersist
504
     * @ORM\PreUpdate
505
     * @param \DateTime|null $updatedAt
506 2
     * @return $this
507
     */
508
    public function setUpdatedAt($updatedAt = null)
509
    {
510
        if ($updatedAt instanceof \DateTime) {
511
            $this->updatedAt = $updatedAt;
512 3
        } else {
513
            $this->updatedAt = new \DateTime('now');
514 3
        }
515 1
516
        return $this;
517
    }
518 3
519
    /**
520
     * {@inheritdoc}
521 1
     */
522
    public function getAllowedGrantTypes()
523 1
    {
524 1
        if ($this->getMetadata()) {
525 1
            return $this->getMetadata()->getGrantTypes();
526 1
        }
527
528
        return parent::getAllowedGrantTypes();
529
    }
530 1
531
    public function ownsDomain($domain)
0 ignored issues
show
Coding Style introduced by
function ownsDomain() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
532
    {
533 1
        foreach ($this->getRedirectUris() as $redirectUrl) {
534
            $host = parse_url($redirectUrl, PHP_URL_HOST);
535 1
            if ($host == $domain) {
536 1
                return true;
537
            }
538
        }
539 1
540 1
        return false;
541 1
    }
542 1
543 1
    public function getContacts()
544
    {
545
        if ($this->getMetadata()) {
546
            return $this->getMetadata()->getContacts();
547
        }
548
549
        return array_map(
550
            function (PersonInterface $owner) {
551
                return $owner->getEmail();
552
            },
553
            $this->getOwners()
554
        );
555
    }
556
557
    /**
558
     * @JMS\VirtualProperty()
559
     * @JMS\SerializedName("client_id")
560
     * @JMS\Groups({"remote_claim"})
561
     * @inheritDoc
562
     */
563
    public function getPublicId()
564
    {
565
        return parent::getPublicId();
566
    }
567
568
    /**
569
     * @JMS\VirtualProperty()
570
     * @JMS\SerializedName("client_uri")
571
     * @JMS\Groups({"remote_claim"})
572
     */
573
    public function getClientUri()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
574
    {
575
        return $this->getSiteUrl();
576
    }
577
}
578