Passed
Push — issue#767 ( 39899e...c3d4bb )
by Guilherme
10:59
created

Client::isPublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\OAuthBundle\Entity;
4
5
use LoginCidadao\CoreBundle\Entity\Authorization;
6
use FOS\OAuthServerBundle\Entity\Client as BaseClient;
7
use Doctrine\ORM\Mapping as ORM;
8
use LoginCidadao\CoreBundle\Model\PersonInterface;
9
use Symfony\Component\HttpFoundation\File\File;
10
use Symfony\Component\Validator\Constraints as Assert;
11
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
12
use JMS\Serializer\Annotation as JMS;
13
use OAuth2\OAuth2;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use LoginCidadao\CoreBundle\Entity\Person;
16
use LoginCidadao\CoreBundle\Model\AbstractUniqueEntity;
0 ignored issues
show
Bug introduced by
The type LoginCidadao\CoreBundle\Model\AbstractUniqueEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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