Passed
Push — issue#767 ( c7fd2d...902b86 )
by Guilherme
05:03
created

Organization::setDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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 Doctrine\ORM\Mapping as ORM;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\OAuthBundle\Model\ClientInterface;
17
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;
18
use LoginCidadao\OAuthBundle\Validator\Constraints\DomainOwnership;
19
use LoginCidadao\OAuthBundle\Validator\Constraints\SectorIdentifier;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
22
23
/**
24
 * @ORM\Entity(repositoryClass="LoginCidadao\OAuthBundle\Entity\OrganizationRepository")
25
 * @ORM\Table(name="organization")
26
 * @ORM\HasLifecycleCallbacks()
27
 * @UniqueEntity("domain")
28
 * @UniqueEntity("name")
29
 * @DomainOwnership
30
 * @SectorIdentifier
31
 */
32
class Organization implements OrganizationInterface
33
{
34
    /**
35
     * @ORM\Id
36
     * @ORM\Column(type="integer")
37
     * @ORM\GeneratedValue(strategy="AUTO")
38
     */
39
    protected $id;
40
41
    /**
42
     * @ORM\Column(name="name", type="string", nullable=false, unique=true)
43
     * @Assert\NotBlank
44
     * @var string
45
     */
46
    protected $name;
47
48
    /**
49
     * @var PersonInterface[]
50
     * @ORM\ManyToMany(targetEntity="LoginCidadao\CoreBundle\Model\PersonInterface")
51
     * @ORM\JoinTable(name="person_organizations",
52
     *      joinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id")},
53
     *      inverseJoinColumns={@ORM\JoinColumn(name="person_id", referencedColumnName="id")}
54
     * )
55
     */
56
    protected $members;
57
58
    /**
59
     * @ORM\Column(name="verified_at", type="datetime", nullable=true)
60
     * @var \DateTime
61
     */
62
    protected $verifiedAt;
63
64
    /**
65
     * @ORM\Column(name="domain", type="string", nullable=false, unique=true)
66
     * @var string
67
     */
68
    protected $domain;
69
70
    /**
71
     * @ORM\OneToMany(targetEntity="LoginCidadao\OpenIDBundle\Entity\ClientMetadata", mappedBy="organization")
72
     * @var ClientInterface
73
     */
74
    protected $clients;
75
76
    /**
77
     * @Assert\Url
78
     * @ORM\Column(name="validation_url", type="string", nullable=true, unique=true)
79
     * @var string
80
     */
81
    protected $validationUrl;
82
83
    /**
84
     * @ORM\Column(name="validation_secret", type="string", nullable=true)
85
     * @var string
86
     */
87
    protected $validationSecret;
88
89
    /**
90
     * @ORM\Column(name="validated_url", type="string", nullable=true)
91
     * @var string
92
     */
93
    protected $validatedUrl;
94
95
    /**
96
     * @ORM\Column(name="trusted", type="boolean", nullable=false)
97
     * @var boolean
98
     */
99
    protected $trusted;
100
101
    /**
102
     * @Assert\Url
103
     * @ORM\Column(name="sector_identifier_uri", type="string", nullable=true, unique=true)
104
     * @var string
105
     */
106
    protected $sectorIdentifierUri;
107
108 3
    public function __construct()
109
    {
110 3
        $this->members = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type LoginCidadao\CoreBundle\Model\PersonInterface[] of property $members.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111 3
        $this->clients = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type LoginCidadao\OAuthBundle\Model\ClientInterface of property $clients.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112 3
        $this->initializeValidationCode();
113 3
        $this->trusted = false;
114 3
    }
115
116 1
    public function getId()
117
    {
118 1
        return $this->id;
119
    }
120
121 1
    public function setId($id)
122
    {
123 1
        $this->id = $id;
124
125 1
        return $this;
126
    }
127
128 1
    public function getName()
129
    {
130 1
        return $this->name;
131
    }
132
133 1
    public function setName($name)
134
    {
135 1
        $this->name = $name;
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * @return PersonInterface[]
142
     */
143 1
    public function getMembers()
144
    {
145 1
        return $this->members;
146
    }
147
148
    /**
149
     * @param PersonInterface[] $members
150
     * @return Organization
151
     */
152 1
    public function setMembers(array $members)
153
    {
154 1
        $this->members = $members;
155
156 1
        return $this;
157
    }
158
159
    /**
160
     * @return \DateTime
161
     */
162 1
    public function getVerifiedAt()
163
    {
164 1
        return $this->verifiedAt;
165
    }
166
167
    /**
168
     * @param \DateTime $verifiedAt
169
     * @return Organization
170
     */
171 1
    public function setVerifiedAt($verifiedAt)
172
    {
173 1
        $this->verifiedAt = $verifiedAt;
174
175 1
        return $this;
176
    }
177
178 1
    public function getDomain()
179
    {
180 1
        return $this->domain;
181
    }
182
183 1
    public function setDomain($domain)
184
    {
185 1
        $this->domain = $domain;
186
187 1
        return $this;
188
    }
189
190 1
    public function getClients()
191
    {
192 1
        return $this->clients;
193
    }
194
195 1
    public function setClients(array $clients)
196
    {
197 1
        $this->clients = $clients;
0 ignored issues
show
Documentation Bug introduced by
It seems like $clients of type array is incompatible with the declared type LoginCidadao\OAuthBundle\Model\ClientInterface of property $clients.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
198
199 1
        return $this;
200
    }
201
202 1
    public function getValidationUrl()
203
    {
204 1
        return $this->validationUrl;
205
    }
206
207 1
    public function setValidationUrl($validationUrl)
208
    {
209 1
        $this->validationUrl = $validationUrl;
210
211 1
        return $this;
212
    }
213
214 1
    public function getValidationSecret()
215
    {
216 1
        $this->initializeValidationCode();
217
218 1
        return $this->validationSecret;
219
    }
220
221 3
    public function setValidationSecret($validationSecret)
222
    {
223 3
        $this->validationSecret = $validationSecret;
224
225 3
        return $this;
226
    }
227
228 1
    public function checkValidation()
229
    {
230 1
        if ($this->validatedUrl !== $this->getValidationUrl()) {
231 1
            $this->setVerifiedAt(null);
232 1
            $this->validatedUrl = null;
233
234 1
            return false;
235
        }
236
237 1
        return true;
238
    }
239
240 1
    public function setValidatedUrl($validatedUrl)
241
    {
242 1
        $this->validatedUrl = $validatedUrl;
243
244 1
        return $this;
245
    }
246
247 1
    public function __toString()
248
    {
249 1
        return $this->getName();
250
    }
251
252 1
    public function isVerified()
253
    {
254 1
        return $this->getVerifiedAt() instanceof \DateTime;
255
    }
256
257 1
    public function isTrusted()
258
    {
259 1
        return $this->trusted;
260
    }
261
262
    /**
263
     *
264
     * @param boolean $trusted
265
     * @return \LoginCidadao\OAuthBundle\Entity\Organization
266
     */
267 1
    public function setTrusted($trusted)
268
    {
269 1
        $this->trusted = $trusted;
270
271 1
        return $this;
272
    }
273
274
    /**
275
     * @return string
276
     */
277 1
    public function getSectorIdentifierUri()
278
    {
279 1
        return $this->sectorIdentifierUri;
280
    }
281
282
    /**
283
     * @param string $sectorIdentifierUri
284
     * @return Organization
285
     */
286 1
    public function setSectorIdentifierUri($sectorIdentifierUri)
287
    {
288 1
        $this->sectorIdentifierUri = $sectorIdentifierUri;
289
290 1
        return $this;
291
    }
292
293
    /**
294
     * @ORM\PrePersist
295
     * @ORM\PreUpdate
296
     */
297 3
    private function initializeValidationCode()
298
    {
299 3
        if ($this->validationSecret) {
300 1
            return;
301
        }
302 3
        $random = base64_encode(random_bytes(35));
303 3
        $this->setValidationSecret($random);
304 3
    }
305
306
307
}
308