| Total Complexity | 65 |
| Total Lines | 554 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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; |
||
|
|
|||
| 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 | public function __construct() |
||
| 162 | { |
||
| 163 | parent::__construct(); |
||
| 164 | $this->authorizations = []; |
||
| 165 | $this->owners = new ArrayCollection(); |
||
| 166 | |||
| 167 | $this->allowedScopes = array( |
||
| 168 | 'public_profile', |
||
| 169 | 'openid', |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | public static function getAllGrants() |
||
| 174 | { |
||
| 175 | return array( |
||
| 176 | OAuth2::GRANT_TYPE_AUTH_CODE, |
||
| 177 | OAuth2::GRANT_TYPE_IMPLICIT, |
||
| 178 | OAuth2::GRANT_TYPE_USER_CREDENTIALS, |
||
| 179 | OAuth2::GRANT_TYPE_CLIENT_CREDENTIALS, |
||
| 180 | OAuth2::GRANT_TYPE_REFRESH_TOKEN, |
||
| 181 | OAuth2::GRANT_TYPE_EXTENSIONS, |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getName() |
||
| 189 | { |
||
| 190 | if ($this->getMetadata()) { |
||
| 191 | if ($this->getMetadata()->getClientName() === null && |
||
| 192 | $this->name !== null |
||
| 193 | ) { |
||
| 194 | $this->getMetadata()->setClientName($this->name); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this->getMetadata()->getClientName(); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->name; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function setName($name) |
||
| 204 | { |
||
| 205 | if ($this->getMetadata()) { |
||
| 206 | $this->getMetadata()->setClientName($name); |
||
| 207 | } |
||
| 208 | $this->name = $name; |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getDescription() |
||
| 214 | { |
||
| 215 | return $this->description; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function setDescription($description) |
||
| 219 | { |
||
| 220 | $this->description = $description; |
||
| 221 | |||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function getSiteUrl() |
||
| 226 | { |
||
| 227 | if ($this->getMetadata()) { |
||
| 228 | return $this->getMetadata()->getClientUri(); |
||
| 229 | } |
||
| 230 | |||
| 231 | return $this->siteUrl; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function setSiteUrl($url) |
||
| 235 | { |
||
| 236 | if ($this->getMetadata()) { |
||
| 237 | $this->getMetadata()->setClientUri($url); |
||
| 238 | } |
||
| 239 | $this->siteUrl = $url; |
||
| 240 | |||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param array|Authorization[]|ArrayCollection $authorizations |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function setAuthorizations($authorizations = []) |
||
| 249 | { |
||
| 250 | $this->authorizations = $authorizations; |
||
| 251 | |||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return array|Authorization[] |
||
| 257 | */ |
||
| 258 | public function getAuthorizations() |
||
| 259 | { |
||
| 260 | return $this->authorizations; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param Authorization $authorization |
||
| 265 | */ |
||
| 266 | public function removeAuthorization(Authorization $authorization) |
||
| 267 | { |
||
| 268 | foreach ($this->authorizations as $k => $candidate) { |
||
| 269 | if ($candidate->getId() === $authorization->getId()) { |
||
| 270 | unset($this->authorizations[$k]); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public function getLandingPageUrl() |
||
| 276 | { |
||
| 277 | if ($this->getMetadata()) { |
||
| 278 | return $this->getMetadata()->getInitiateLoginUri(); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this->landingPageUrl; |
||
| 282 | } |
||
| 283 | |||
| 284 | public function setLandingPageUrl($landingPageUrl) |
||
| 285 | { |
||
| 286 | if ($this->getMetadata()) { |
||
| 287 | $this->getMetadata()->setInitiateLoginUri($landingPageUrl); |
||
| 288 | } |
||
| 289 | $this->landingPageUrl = $landingPageUrl; |
||
| 290 | |||
| 291 | return $this; |
||
| 292 | } |
||
| 293 | |||
| 294 | public function getTermsOfUseUrl() |
||
| 295 | { |
||
| 296 | if ($this->getMetadata()) { |
||
| 297 | return $this->getMetadata()->getTosUri(); |
||
| 298 | } |
||
| 299 | |||
| 300 | return $this->termsOfUseUrl; |
||
| 301 | } |
||
| 302 | |||
| 303 | public function setTermsOfUseUrl($termsOfUseUrl) |
||
| 304 | { |
||
| 305 | if ($this->getMetadata()) { |
||
| 306 | $this->getMetadata()->setTosUri($termsOfUseUrl); |
||
| 307 | } |
||
| 308 | $this->termsOfUseUrl = $termsOfUseUrl; |
||
| 309 | |||
| 310 | return $this; |
||
| 311 | } |
||
| 312 | |||
| 313 | public function getAllowedScopes() |
||
| 314 | { |
||
| 315 | $scopes = ['public_profile', 'openid']; |
||
| 316 | |||
| 317 | if (is_array($this->allowedScopes)) { |
||
| 318 | $scopes = $this->allowedScopes; |
||
| 319 | } |
||
| 320 | |||
| 321 | return $scopes; |
||
| 322 | } |
||
| 323 | |||
| 324 | public function setAllowedScopes(array $allowedScopes) |
||
| 325 | { |
||
| 326 | $this->allowedScopes = $allowedScopes; |
||
| 327 | |||
| 328 | return $this; |
||
| 329 | } |
||
| 330 | |||
| 331 | public function isVisible() |
||
| 332 | { |
||
| 333 | return $this->visible; |
||
| 334 | } |
||
| 335 | |||
| 336 | public function setVisible($visible) |
||
| 337 | { |
||
| 338 | $this->visible = $visible; |
||
| 339 | |||
| 340 | return $this; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function isPublished() |
||
| 344 | { |
||
| 345 | return $this->published; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function setPublished($published) |
||
| 349 | { |
||
| 350 | $this->published = $published; |
||
| 351 | |||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function setId($var) |
||
| 356 | { |
||
| 357 | $this->id = $var; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | public function getOwners() |
||
| 363 | { |
||
| 364 | return $this->owners; |
||
| 365 | } |
||
| 366 | |||
| 367 | /* Unique Interface Stuff */ |
||
| 368 | |||
| 369 | public function setOwners($owners) |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Gets the Unique Id of the Entity. |
||
| 378 | * @return string the entity UID |
||
| 379 | */ |
||
| 380 | public function getUid() |
||
| 381 | { |
||
| 382 | 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 | public function setUid($uid = null) |
||
| 391 | { |
||
| 392 | $this->uid = $uid; |
||
| 393 | |||
| 394 | return $this; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @inheritDoc |
||
| 399 | */ |
||
| 400 | public function setClientId($clientId) |
||
| 401 | { |
||
| 402 | $parts = explode('_', $clientId, 2); |
||
| 403 | $this->setId($parts[0]); |
||
| 404 | $this->setRandomId($parts[1]); |
||
| 405 | |||
| 406 | return $this; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Compatibility with OIDC code |
||
| 411 | */ |
||
| 412 | public function getClientId() |
||
| 413 | { |
||
| 414 | return $this->getPublicId(); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Compatibility with OIDC code |
||
| 419 | */ |
||
| 420 | public function getClientSecret() |
||
| 421 | { |
||
| 422 | return $this->getSecret(); |
||
| 423 | } |
||
| 424 | |||
| 425 | public function getMetadata() |
||
| 426 | { |
||
| 427 | return $this->metadata; |
||
| 428 | } |
||
| 429 | |||
| 430 | public function setMetadata(ClientMetadata $metadata) |
||
| 431 | { |
||
| 432 | $this->metadata = $metadata; |
||
| 433 | |||
| 434 | return $this; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @JMS\VirtualProperty() |
||
| 439 | * @JMS\SerializedName("redirect_uris") |
||
| 440 | * @JMS\Groups({"remote_claim"}) |
||
| 441 | * @return array|string[] |
||
| 442 | */ |
||
| 443 | public function getRedirectUris() |
||
| 444 | { |
||
| 445 | if ($this->getMetadata()) { |
||
| 446 | return $this->getMetadata()->getRedirectUris(); |
||
| 447 | } |
||
| 448 | |||
| 449 | return parent::getRedirectUris(); |
||
| 450 | } |
||
| 451 | |||
| 452 | public function setRedirectUris(array $redirectUris) |
||
| 453 | { |
||
| 454 | if ($this->getMetadata()) { |
||
| 455 | $this->getMetadata()->setRedirectUris($redirectUris); |
||
| 456 | } else { |
||
| 457 | parent::setRedirectUris($redirectUris); |
||
| 458 | } |
||
| 459 | |||
| 460 | return $this; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return File |
||
| 465 | */ |
||
| 466 | public function getImage() |
||
| 467 | { |
||
| 468 | 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 | public function setImage($image) |
||
| 482 | { |
||
| 483 | $this->image = $image; |
||
| 484 | |||
| 485 | if ($this->image) { |
||
| 486 | $this->updatedAt = new \DateTime('now'); |
||
| 487 | } |
||
| 488 | |||
| 489 | return $this; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | public function getImageName() |
||
| 496 | { |
||
| 497 | return $this->imageName; |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $imageName |
||
| 502 | * @return $this |
||
| 503 | */ |
||
| 504 | public function setImageName($imageName) |
||
| 505 | { |
||
| 506 | $this->imageName = $imageName; |
||
| 507 | |||
| 508 | return $this; |
||
| 509 | } |
||
| 510 | |||
| 511 | public function getUpdatedAt() |
||
| 512 | { |
||
| 513 | return $this->updatedAt; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @ORM\PrePersist |
||
| 518 | * @ORM\PreUpdate |
||
| 519 | * @param \DateTime|null $updatedAt |
||
| 520 | * @return $this |
||
| 521 | */ |
||
| 522 | public function setUpdatedAt($updatedAt = null) |
||
| 523 | { |
||
| 524 | if ($updatedAt instanceof \DateTime) { |
||
| 525 | $this->updatedAt = $updatedAt; |
||
| 526 | } else { |
||
| 527 | $this->updatedAt = new \DateTime('now'); |
||
| 528 | } |
||
| 529 | |||
| 530 | return $this; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * {@inheritdoc} |
||
| 535 | */ |
||
| 536 | public function getAllowedGrantTypes() |
||
| 537 | { |
||
| 538 | if ($this->getMetadata()) { |
||
| 539 | return $this->getMetadata()->getGrantTypes(); |
||
| 540 | } |
||
| 541 | |||
| 542 | return parent::getAllowedGrantTypes(); |
||
| 543 | } |
||
| 544 | |||
| 545 | public function ownsDomain($domain) |
||
| 546 | { |
||
| 547 | foreach ($this->getRedirectUris() as $redirectUrl) { |
||
| 548 | $host = parse_url($redirectUrl, PHP_URL_HOST); |
||
| 549 | if ($host == $domain) { |
||
| 550 | return true; |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | return false; |
||
| 555 | } |
||
| 556 | |||
| 557 | public function getContacts() |
||
| 558 | { |
||
| 559 | if ($this->getMetadata()) { |
||
| 560 | return $this->getMetadata()->getContacts(); |
||
| 561 | } |
||
| 562 | |||
| 563 | return array_map( |
||
| 564 | function (PersonInterface $owner) { |
||
| 565 | return $owner->getEmail(); |
||
| 566 | }, |
||
| 567 | $this->getOwners() |
||
| 568 | ); |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @JMS\VirtualProperty() |
||
| 573 | * @JMS\SerializedName("client_id") |
||
| 574 | * @JMS\Groups({"remote_claim"}) |
||
| 575 | * @inheritDoc |
||
| 576 | */ |
||
| 577 | public function 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 |