Failed Conditions
Branch issue#666 (91903a)
by Guilherme
08:25
created

Client::getOwners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
159 37
    public function __construct()
160
    {
161 37
        parent::__construct();
162 37
        $this->authorizations = [];
163 37
        $this->owners = [];
164
165 37
        $this->allowedScopes = array(
166
            'public_profile',
167
            'openid',
168
        );
169 37
    }
170
171 1
    public static function getAllGrants()
172
    {
173
        return array(
174 1
            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
183
    /**
184
     * @return string
185
     */
186 8
    public function getName()
187
    {
188 8
        if ($this->getMetadata()) {
189 2
            if ($this->getMetadata()->getClientName() === null &&
190 2
                $this->name !== null
191
            ) {
192 1
                $this->getMetadata()->setClientName($this->name);
193
            }
194
195 2
            return $this->getMetadata()->getClientName();
196
        }
197
198 7
        return $this->name;
199
    }
200
201 13
    public function setName($name)
202
    {
203 13
        if ($this->getMetadata()) {
204 1
            $this->getMetadata()->setClientName($name);
205
        }
206 13
        $this->name = $name;
207
208 13
        return $this;
209
    }
210
211 2
    public function getDescription()
212
    {
213 2
        return $this->description;
214
    }
215
216 2
    public function setDescription($description)
217
    {
218 2
        $this->description = $description;
219
220 2
        return $this;
221
    }
222
223 3
    public function getSiteUrl()
224
    {
225 3
        if ($this->getMetadata()) {
226 1
            return $this->getMetadata()->getClientUri();
227
        }
228
229 3
        return $this->siteUrl;
230
    }
231
232 3
    public function setSiteUrl($url)
233
    {
234 3
        if ($this->getMetadata()) {
235 1
            $this->getMetadata()->setClientUri($url);
236
        }
237 3
        $this->siteUrl = $url;
238
239 3
        return $this;
240
    }
241
242
    /**
243
     * @param array|Authorization[] $authorizations
244
     * @return $this
245
     */
246 2
    public function setAuthorizations($authorizations = [])
247
    {
248 2
        $this->authorizations = $authorizations;
249
250 2
        return $this;
251
    }
252
253
    /**
254
     * @return array|Authorization[]
255
     */
256 2
    public function getAuthorizations()
257
    {
258 2
        return $this->authorizations;
259
    }
260
261
    /**
262
     * @param Authorization $authorization
263
     */
264 2
    public function removeAuthorization(Authorization $authorization)
265
    {
266 2
        foreach ($this->authorizations as $k => $candidate) {
267 2
            if ($candidate->getId() === $authorization->getId()) {
268 2
                unset($this->authorizations[$k]);
269
            }
270
        }
271 2
    }
272
273 2
    public function getLandingPageUrl()
274
    {
275 2
        if ($this->getMetadata()) {
276 1
            return $this->getMetadata()->getInitiateLoginUri();
277
        }
278
279 2
        return $this->landingPageUrl;
280
    }
281
282 2
    public function setLandingPageUrl($landingPageUrl)
283
    {
284 2
        if ($this->getMetadata()) {
285 1
            $this->getMetadata()->setInitiateLoginUri($landingPageUrl);
286
        }
287 2
        $this->landingPageUrl = $landingPageUrl;
288
289 2
        return $this;
290
    }
291
292 3
    public function getTermsOfUseUrl()
293
    {
294 3
        if ($this->getMetadata()) {
295 1
            return $this->getMetadata()->getTosUri();
296
        }
297
298 3
        return $this->termsOfUseUrl;
299
    }
300
301 3
    public function setTermsOfUseUrl($termsOfUseUrl)
302
    {
303 3
        if ($this->getMetadata()) {
304 1
            $this->getMetadata()->setTosUri($termsOfUseUrl);
305
        }
306 3
        $this->termsOfUseUrl = $termsOfUseUrl;
307
308 3
        return $this;
309
    }
310
311 3
    public function getAllowedScopes()
312
    {
313 3
        $scopes = ['public_profile', 'openid'];
314
315 3
        if (is_array($this->allowedScopes)) {
0 ignored issues
show
introduced by
The condition is_array($this->allowedScopes) can never be false.
Loading history...
316 3
            $scopes = $this->allowedScopes;
317
        }
318
319 3
        return $scopes;
320
    }
321
322 8
    public function setAllowedScopes(array $allowedScopes)
323
    {
324 8
        $this->allowedScopes = $allowedScopes;
325
326 8
        return $this;
327
    }
328
329 1
    public function isVisible()
330
    {
331 1
        return $this->visible;
332
    }
333
334 2
    public function setVisible($visible)
335
    {
336 2
        $this->visible = $visible;
337
338 2
        return $this;
339
    }
340
341 1
    public function isPublished()
342
    {
343 1
        return $this->published;
344
    }
345
346 2
    public function setPublished($published)
347
    {
348 2
        $this->published = $published;
349
350 2
        return $this;
351
    }
352
353 17
    public function setId($var)
354
    {
355 17
        $this->id = $var;
356
357 17
        return $this;
358
    }
359
360 2
    public function getOwners()
361
    {
362 2
        return $this->owners;
363
    }
364
365
    /* Unique Interface Stuff */
366
367 2
    public function setOwners($owners)
368
    {
369 2
        $this->owners = $owners;
370
371 2
        return $this;
372
    }
373
374
    /**
375
     * Gets the Unique Id of the Entity.
376
     * @return string the entity UID
377
     */
378 1
    public function getUid()
379
    {
380 1
        return $this->uid;
381
    }
382
383
    /**
384
     * Sets the Unique Id of the Entity.
385
     * @param string $uid the entity UID
386
     * @return $this
387
     */
388 1
    public function setUid($uid = null)
389
    {
390 1
        $this->uid = $uid;
391
392 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LoginCidadao\OAuthBundle\Entity\Client which is incompatible with the return type mandated by LoginCidadao\CoreBundle\...tityInterface::setUid() of LoginCidadao\CoreBundle\Model\AbstractUniqueEntity.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
393
    }
394
395
    /**
396
     * @inheritDoc
397
     */
398 9
    public function setClientId($clientId)
399
    {
400 9
        $parts = explode('_', $clientId, 2);
401 9
        $this->setId($parts[0]);
402 9
        $this->setRandomId($parts[1]);
403
404 9
        return $this;
405
    }
406
407
    /**
408
     * Compatibility with OIDC code
409
     */
410 7
    public function getClientId()
411
    {
412 7
        return $this->getPublicId();
413
    }
414
415
    /**
416
     * Compatibility with OIDC code
417
     */
418 4
    public function getClientSecret()
419
    {
420 4
        return $this->getSecret();
421
    }
422
423 23
    public function getMetadata()
424
    {
425 23
        return $this->metadata;
426
    }
427
428 5
    public function setMetadata(ClientMetadata $metadata)
429
    {
430 5
        $this->metadata = $metadata;
431
432 5
        return $this;
433
    }
434
435
    /**
436
     * @JMS\VirtualProperty()
437
     * @JMS\SerializedName("redirect_uris")
438
     * @JMS\Groups({"remote_claim"})
439
     * @return array|string[]
440
     */
441 9
    public function getRedirectUris()
442
    {
443 9
        if ($this->getMetadata()) {
444 1
            return $this->getMetadata()->getRedirectUris();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMetadata()->getRedirectUris() returns the type string which is incompatible with the documented return type array|string[].
Loading history...
445
        }
446
447 9
        return parent::getRedirectUris();
448
    }
449
450 19
    public function setRedirectUris(array $redirectUris)
451
    {
452 19
        if ($this->getMetadata()) {
453 1
            $this->getMetadata()->setRedirectUris($redirectUris);
454
        } else {
455 19
            parent::setRedirectUris($redirectUris);
456
        }
457
458 19
        return $this;
459
    }
460
461
    /**
462
     * @return File
463
     */
464 2
    public function getImage()
465
    {
466 2
        return $this->image;
467
    }
468
469
    /**
470
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
471
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
472
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
473
     * must be able to accept an instance of 'File' as the bundle will inject one here
474
     * during Doctrine hydration.
475
     *
476
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
477
     * @return $this
478
     */
479 1
    public function setImage($image)
480
    {
481 1
        $this->image = $image;
482
483 1
        if ($this->image) {
484 1
            $this->updatedAt = new \DateTime('now');
485
        }
486
487 1
        return $this;
488
    }
489
490
    /**
491
     * @return string
492
     */
493 1
    public function getImageName()
494
    {
495 1
        return $this->imageName;
496
    }
497
498
    /**
499
     * @param string $imageName
500
     * @return $this
501
     */
502 1
    public function setImageName($imageName)
503
    {
504 1
        $this->imageName = $imageName;
505
506 1
        return $this;
507
    }
508
509 1
    public function getUpdatedAt()
510
    {
511 1
        return $this->updatedAt;
512
    }
513
514
    /**
515
     * @ORM\PrePersist
516
     * @ORM\PreUpdate
517
     * @param \DateTime|null $updatedAt
518
     * @return $this
519
     */
520 2
    public function setUpdatedAt($updatedAt = null)
521
    {
522 2
        if ($updatedAt instanceof \DateTime) {
523 1
            $this->updatedAt = $updatedAt;
524
        } else {
525 2
            $this->updatedAt = new \DateTime('now');
526
        }
527
528 2
        return $this;
529
    }
530
531
    /**
532
     * {@inheritdoc}
533
     */
534 3
    public function getAllowedGrantTypes()
535
    {
536 3
        if ($this->getMetadata()) {
537 1
            return $this->getMetadata()->getGrantTypes();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMetadata()->getGrantTypes() returns the type string which is incompatible with the return type mandated by FOS\OAuthServerBundle\Mo...:getAllowedGrantTypes() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
538
        }
539
540 3
        return parent::getAllowedGrantTypes();
541
    }
542
543 1
    public function ownsDomain($domain)
544
    {
545 1
        foreach ($this->getRedirectUris() as $redirectUrl) {
546 1
            $host = parse_url($redirectUrl, PHP_URL_HOST);
547 1
            if ($host == $domain) {
548 1
                return true;
549
            }
550
        }
551
552 1
        return false;
553
    }
554
555 1
    public function getContacts()
556
    {
557 1
        if ($this->getMetadata()) {
558 1
            return $this->getMetadata()->getContacts();
559
        }
560
561 1
        return array_map(
562 1
            function (PersonInterface $owner) {
563 1
                return $owner->getEmail();
564 1
            },
565 1
            $this->getOwners()
566
        );
567
    }
568
569
    /**
570
     * @JMS\VirtualProperty()
571
     * @JMS\SerializedName("client_id")
572
     * @JMS\Groups({"remote_claim"})
573
     * @inheritDoc
574
     */
575 10
    public function getPublicId()
576
    {
577 10
        return parent::getPublicId();
578
    }
579
580
    /**
581
     * @JMS\VirtualProperty()
582
     * @JMS\SerializedName("client_uri")
583
     * @JMS\Groups({"remote_claim"})
584
     */
585
    public function getClientUri()
586
    {
587
        return $this->getSiteUrl();
588
    }
589
}
590