Client   F
last analyzed

Complexity

Total Complexity 65

Size/Duplication

Total Lines 554
Duplicated Lines 0 %

Test Coverage

Coverage 98.76%

Importance

Changes 0
Metric Value
eloc 138
dl 0
loc 554
ccs 159
cts 161
cp 0.9876
rs 3.2
c 0
b 0
f 0
wmc 65

44 Methods

Rating   Name   Duplication   Size   Complexity  
A getPublicId() 0 3 1
A getMetadata() 0 3 1
A getImage() 0 3 1
A getOwners() 0 3 1
A getUid() 0 3 1
A isPublished() 0 3 1
A getDescription() 0 3 1
A isVisible() 0 3 1
A getClientUri() 0 3 1
A getClientSecret() 0 3 1
A getImageName() 0 3 1
A getAuthorizations() 0 3 1
A getUpdatedAt() 0 3 1
A getClientId() 0 3 1
A setOwners() 0 5 1
A ownsDomain() 0 10 3
A setClientId() 0 7 1
A getLandingPageUrl() 0 7 2
A setDescription() 0 5 1
A setVisible() 0 5 1
A setPublished() 0 5 1
A removeAuthorization() 0 5 3
A setUpdatedAt() 0 9 2
A setAuthorizations() 0 5 1
A setUid() 0 5 1
A setMetadata() 0 5 1
A setTermsOfUseUrl() 0 8 2
A setLandingPageUrl() 0 8 2
A getRedirectUris() 0 7 2
A __construct() 0 9 1
A setId() 0 5 1
A getAllowedGrantTypes() 0 7 2
A getName() 0 13 4
A getTermsOfUseUrl() 0 7 2
A setAllowedScopes() 0 5 1
A getAllGrants() 0 9 1
A setRedirectUris() 0 9 2
A getAllowedScopes() 0 9 2
A setImageName() 0 5 1
A getSiteUrl() 0 7 2
A setName() 0 8 2
A setSiteUrl() 0 8 2
A setImage() 0 9 2
A getContacts() 0 11 2

How to fix   Complexity   

Complex Class

Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.

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 Doctrine\Common\Collections\ArrayCollection;
14
use LoginCidadao\CoreBundle\Entity\Authorization;
15
use FOS\OAuthServerBundle\Entity\Client as BaseClient;
16
use Doctrine\ORM\Mapping as ORM;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
19
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface;
20
use Symfony\Component\HttpFoundation\File\File;
21
use Symfony\Component\Validator\Constraints as Assert;
22
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
23
use JMS\Serializer\Annotation as JMS;
24
use OAuth2\OAuth2;
25
use LoginCidadao\OAuthBundle\Model\ClientInterface;
26
use Vich\UploaderBundle\Mapping\Annotation as Vich;
27
28
/**
29
 * @ORM\Entity(repositoryClass="LoginCidadao\OAuthBundle\Entity\ClientRepository")
30
 * @ORM\Table(name="client")
31
 * @ORM\HasLifecycleCallbacks
32
 * @UniqueEntity("name")
33
 * @JMS\ExclusionPolicy("all")
34
 * @Vich\Uploadable
35
 */
36
class Client extends BaseClient implements ClientInterface, ClaimProviderInterface
37
{
38
    /**
39
     * @ORM\Id
40
     * @ORM\Column(type="integer")
41
     * @ORM\GeneratedValue(strategy="AUTO")
42
     * @JMS\Expose
43
     * @JMS\Groups({"public"})
44
     */
45
    protected $id;
46
47
    /**
48
     * @ORM\Column(type="string", nullable=false, unique=true)
49
     * @JMS\Expose
50
     * @JMS\SerializedName("client_name")
51
     * @JMS\Groups({"public", "remote_claim"})
52
     */
53
    private $name;
54
55
    /**
56
     * @ORM\Column(type="string", length=4000, nullable=true)
57
     * @JMS\Expose
58
     * @JMS\Groups({"public"})
59
     */
60
    private $description;
61
62
    /**
63
     * @ORM\Column(type="string", length=2000, nullable=true)
64
     * @JMS\Expose
65
     * @JMS\Groups({"public"})
66
     */
67
    private $landingPageUrl;
68
69
    /**
70
     * @ORM\Column(type="string", length=2000, nullable=true)
71
     * @JMS\Expose
72
     * @JMS\Groups({"public"})
73
     */
74
    private $termsOfUseUrl;
75
76
    /**
77
     * @var string[]|null
78
     * @ORM\Column(type="json_array", nullable=false)
79
     */
80
    private $allowedScopes;
81
82
    /**
83
     * @ORM\OneToMany(targetEntity="LoginCidadao\CoreBundle\Entity\Authorization", mappedBy="client", cascade={"remove"}, orphanRemoval=true)
84
     * @var Authorization[]|ArrayCollection
85
     */
86
    private $authorizations;
87
88
    /**
89
     * @ORM\Column(type="string", length=2000, nullable=true)
90
     * @JMS\Expose
91
     * @JMS\Groups({"public"})
92
     */
93
    private $siteUrl;
94
95
    /**
96
     * @Assert\File(
97
     *      maxSize="2M",
98
     *      maxSizeMessage="The maxmimum allowed file size is 2MB.",
99
     *      mimeTypes={"image/png", "image/jpeg", "image/pjpeg"},
100
     *      mimeTypesMessage="Only JPEG and PNG images are allowed."
101
     * )
102
     * @Vich\UploadableField(mapping="client_image", fileNameProperty="imageName")
103
     * @var File $image
104
     * @JMS\Since("1.0.2")
105
     */
106
    private $image;
107
108
    /**
109
     * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
110
     *
111
     * @var string $imageName
112
     * @JMS\Since("1.0.2")
113
     */
114
    private $imageName;
115
116
    /**
117
     * @ORM\Column(type="boolean", nullable=false)
118
     * @JMS\Expose
119
     * @JMS\Groups({"public"})
120
     */
121
    private $published;
122
123
    /**
124
     * @ORM\Column(type="boolean", nullable=false)
125
     * @JMS\Expose
126
     * @JMS\Groups({"public"})
127
     */
128
    private $visible;
129
130
    /**
131
     * @ORM\ManyToMany(targetEntity="LoginCidadao\CoreBundle\Entity\Person", inversedBy="clients"  )
132
     * @ORM\JoinTable(name="client_owners",
133
     *      joinColumns={@ORM\JoinColumn(name="person_id", referencedColumnName="id")},
134
     *      inverseJoinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")}
135
     *      )
136
     * @var PersonInterface[]|ArrayCollection
137
     */
138
    private $owners;
139
140
    /**
141
     * @ORM\OneToMany(targetEntity="LoginCidadao\APIBundle\Entity\LogoutKey", mappedBy="client")
142
     */
143
    private $logoutKeys;
0 ignored issues
show
introduced by
The private property $logoutKeys is not used, and could be removed.
Loading history...
144
145
    /**
146
     * @var \LoginCidadao\OpenIDBundle\Entity\ClientMetadata
147
     * @ORM\OneToOne(targetEntity="LoginCidadao\OpenIDBundle\Entity\ClientMetadata", mappedBy="client", cascade={"persist"})
148
     */
149
    private $metadata;
150
151
    /**
152
     * @ORM\Column(name="updated_at", type="datetime")
153
     */
154
    private $updatedAt;
155
    /**
156
     * @ORM\Column(type="string", nullable=true, unique=true)
157
     * @var string
158
     */
159
    private $uid;
160
161 71
    public function __construct()
162
    {
163 71
        parent::__construct();
164 71
        $this->authorizations = [];
165 71
        $this->owners = new ArrayCollection();
166
167 71
        $this->allowedScopes = array(
168
            'public_profile',
169
            'openid',
170
        );
171 71
    }
172
173 1
    public static function getAllGrants()
174
    {
175
        return array(
176 1
            OAuth2::GRANT_TYPE_AUTH_CODE,
177 1
            OAuth2::GRANT_TYPE_IMPLICIT,
178 1
            OAuth2::GRANT_TYPE_USER_CREDENTIALS,
179 1
            OAuth2::GRANT_TYPE_CLIENT_CREDENTIALS,
180 1
            OAuth2::GRANT_TYPE_REFRESH_TOKEN,
181 1
            OAuth2::GRANT_TYPE_EXTENSIONS,
182
        );
183
    }
184
185
    /**
186
     * @return string
187
     */
188 13
    public function getName()
189
    {
190 13
        if ($this->getMetadata()) {
191 2
            if ($this->getMetadata()->getClientName() === null &&
192 2
                $this->name !== null
193
            ) {
194 1
                $this->getMetadata()->setClientName($this->name);
195
            }
196
197 2
            return $this->getMetadata()->getClientName();
198
        }
199
200 12
        return $this->name;
201
    }
202
203 19
    public function setName($name)
204
    {
205 19
        if ($this->getMetadata()) {
206 1
            $this->getMetadata()->setClientName($name);
207
        }
208 19
        $this->name = $name;
209
210 19
        return $this;
211
    }
212
213 4
    public function getDescription()
214
    {
215 4
        return $this->description;
216
    }
217
218 4
    public function setDescription($description)
219
    {
220 4
        $this->description = $description;
221
222 4
        return $this;
223
    }
224
225 7
    public function getSiteUrl()
226
    {
227 7
        if ($this->getMetadata()) {
228 1
            return $this->getMetadata()->getClientUri();
229
        }
230
231 7
        return $this->siteUrl;
232
    }
233
234 7
    public function setSiteUrl($url)
235
    {
236 7
        if ($this->getMetadata()) {
237 1
            $this->getMetadata()->setClientUri($url);
238
        }
239 7
        $this->siteUrl = $url;
240
241 7
        return $this;
242
    }
243
244
    /**
245
     * @param array|Authorization[]|ArrayCollection $authorizations
246
     * @return $this
247
     */
248 2
    public function setAuthorizations($authorizations = [])
249
    {
250 2
        $this->authorizations = $authorizations;
251
252 2
        return $this;
253
    }
254
255
    /**
256
     * @return array|Authorization[]
257
     */
258 2
    public function getAuthorizations()
259
    {
260 2
        return $this->authorizations;
261
    }
262
263
    /**
264
     * @param Authorization $authorization
265
     */
266 2
    public function removeAuthorization(Authorization $authorization)
267
    {
268 2
        foreach ($this->authorizations as $k => $candidate) {
269 2
            if ($candidate->getId() === $authorization->getId()) {
270 2
                unset($this->authorizations[$k]);
271
            }
272
        }
273 2
    }
274
275 3
    public function getLandingPageUrl()
276
    {
277 3
        if ($this->getMetadata()) {
278 1
            return $this->getMetadata()->getInitiateLoginUri();
279
        }
280
281 3
        return $this->landingPageUrl;
282
    }
283
284 3
    public function setLandingPageUrl($landingPageUrl)
285
    {
286 3
        if ($this->getMetadata()) {
287 1
            $this->getMetadata()->setInitiateLoginUri($landingPageUrl);
288
        }
289 3
        $this->landingPageUrl = $landingPageUrl;
290
291 3
        return $this;
292
    }
293
294 7
    public function getTermsOfUseUrl()
295
    {
296 7
        if ($this->getMetadata()) {
297 1
            return $this->getMetadata()->getTosUri();
298
        }
299
300 7
        return $this->termsOfUseUrl;
301
    }
302
303 7
    public function setTermsOfUseUrl($termsOfUseUrl)
304
    {
305 7
        if ($this->getMetadata()) {
306 1
            $this->getMetadata()->setTosUri($termsOfUseUrl);
307
        }
308 7
        $this->termsOfUseUrl = $termsOfUseUrl;
309
310 7
        return $this;
311
    }
312
313 3
    public function getAllowedScopes()
314
    {
315 3
        $scopes = ['public_profile', 'openid'];
316
317 3
        if (is_array($this->allowedScopes)) {
318 3
            $scopes = $this->allowedScopes;
319
        }
320
321 3
        return $scopes;
322
    }
323
324 11
    public function setAllowedScopes(array $allowedScopes)
325
    {
326 11
        $this->allowedScopes = $allowedScopes;
327
328 11
        return $this;
329
    }
330
331 2
    public function isVisible()
332
    {
333 2
        return $this->visible;
334
    }
335
336 4
    public function setVisible($visible)
337
    {
338 4
        $this->visible = $visible;
339
340 4
        return $this;
341
    }
342
343 2
    public function isPublished()
344
    {
345 2
        return $this->published;
346
    }
347
348 4
    public function setPublished($published)
349
    {
350 4
        $this->published = $published;
351
352 4
        return $this;
353
    }
354
355 29
    public function setId($var)
356
    {
357 29
        $this->id = $var;
358
359 29
        return $this;
360
    }
361
362 5
    public function getOwners()
363
    {
364 5
        return $this->owners;
365
    }
366
367
    /* Unique Interface Stuff */
368
369 2
    public function setOwners($owners)
370
    {
371 2
        $this->owners = $owners;
372
373 2
        return $this;
374
    }
375
376
    /**
377
     * Gets the Unique Id of the Entity.
378
     * @return string the entity UID
379
     */
380 1
    public function getUid()
381
    {
382 1
        return $this->uid;
383
    }
384
385
    /**
386
     * Sets the Unique Id of the Entity.
387
     * @param string $uid the entity UID
388
     * @return ClientInterface
389
     */
390 1
    public function setUid($uid = null)
391
    {
392 1
        $this->uid = $uid;
393
394 1
        return $this;
395
    }
396
397
    /**
398
     * @inheritDoc
399
     */
400 11
    public function setClientId($clientId)
401
    {
402 11
        $parts = explode('_', $clientId, 2);
403 11
        $this->setId($parts[0]);
404 11
        $this->setRandomId($parts[1]);
405
406 11
        return $this;
407
    }
408
409
    /**
410
     * Compatibility with OIDC code
411
     */
412 11
    public function getClientId()
413
    {
414 11
        return $this->getPublicId();
415
    }
416
417
    /**
418
     * Compatibility with OIDC code
419
     */
420 5
    public function getClientSecret()
421
    {
422 5
        return $this->getSecret();
423
    }
424
425 32
    public function getMetadata()
426
    {
427 32
        return $this->metadata;
428
    }
429
430 8
    public function setMetadata(ClientMetadata $metadata)
431
    {
432 8
        $this->metadata = $metadata;
433
434 8
        return $this;
435
    }
436
437
    /**
438
     * @JMS\VirtualProperty()
439
     * @JMS\SerializedName("redirect_uris")
440
     * @JMS\Groups({"remote_claim"})
441
     * @return array|string[]
442
     */
443 14
    public function getRedirectUris()
444
    {
445 14
        if ($this->getMetadata()) {
446 1
            return $this->getMetadata()->getRedirectUris();
447
        }
448
449 14
        return parent::getRedirectUris();
450
    }
451
452 23
    public function setRedirectUris(array $redirectUris)
453
    {
454 23
        if ($this->getMetadata()) {
455 1
            $this->getMetadata()->setRedirectUris($redirectUris);
456
        } else {
457 23
            parent::setRedirectUris($redirectUris);
458
        }
459
460 23
        return $this;
461
    }
462
463
    /**
464
     * @return File
465
     */
466 1
    public function getImage()
467
    {
468 1
        return $this->image;
469
    }
470
471
    /**
472
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
473
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
474
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
475
     * must be able to accept an instance of 'File' as the bundle will inject one here
476
     * during Doctrine hydration.
477
     *
478
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
479
     * @return $this
480
     */
481 1
    public function setImage($image)
482
    {
483 1
        $this->image = $image;
484
485 1
        if ($this->image) {
486 1
            $this->updatedAt = new \DateTime('now');
487
        }
488
489 1
        return $this;
490
    }
491
492
    /**
493
     * @return string
494
     */
495 1
    public function getImageName()
496
    {
497 1
        return $this->imageName;
498
    }
499
500
    /**
501
     * @param string $imageName
502
     * @return $this
503
     */
504 1
    public function setImageName($imageName)
505
    {
506 1
        $this->imageName = $imageName;
507
508 1
        return $this;
509
    }
510
511 1
    public function getUpdatedAt()
512
    {
513 1
        return $this->updatedAt;
514
    }
515
516
    /**
517
     * @ORM\PrePersist
518
     * @ORM\PreUpdate
519
     * @param \DateTime|null $updatedAt
520
     * @return $this
521
     */
522 1
    public function setUpdatedAt($updatedAt = null)
523
    {
524 1
        if ($updatedAt instanceof \DateTime) {
525 1
            $this->updatedAt = $updatedAt;
526
        } else {
527 1
            $this->updatedAt = new \DateTime('now');
528
        }
529
530 1
        return $this;
531
    }
532
533
    /**
534
     * {@inheritdoc}
535
     */
536 4
    public function getAllowedGrantTypes()
537
    {
538 4
        if ($this->getMetadata()) {
539 1
            return $this->getMetadata()->getGrantTypes();
540
        }
541
542 4
        return parent::getAllowedGrantTypes();
543
    }
544
545 1
    public function ownsDomain($domain)
546
    {
547 1
        foreach ($this->getRedirectUris() as $redirectUrl) {
548 1
            $host = parse_url($redirectUrl, PHP_URL_HOST);
549 1
            if ($host == $domain) {
550 1
                return true;
551
            }
552
        }
553
554 1
        return false;
555
    }
556
557 1
    public function getContacts()
558
    {
559 1
        if ($this->getMetadata()) {
560 1
            return $this->getMetadata()->getContacts();
561
        }
562
563 1
        return array_map(
564
            function (PersonInterface $owner) {
565 1
                return $owner->getEmail();
566 1
            },
567 1
            $this->getOwners()
568
        );
569
    }
570
571
    /**
572
     * @JMS\VirtualProperty()
573
     * @JMS\SerializedName("client_id")
574
     * @JMS\Groups({"remote_claim"})
575
     * @inheritDoc
576
     */
577 16
    public function getPublicId()
578
    {
579 16
        return parent::getPublicId();
580
    }
581
582
    /**
583
     * @JMS\VirtualProperty()
584
     * @JMS\SerializedName("client_uri")
585
     * @JMS\Groups({"remote_claim"})
586
     */
587
    public function getClientUri()
588
    {
589
        return $this->getSiteUrl();
590
    }
591
}
592