Failed Conditions
Push — issue#765 ( 54a2c2...25bda7 )
by Guilherme
09:33
created

Organization::setName()   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 LoginCidadao\CoreBundle\Model\PersonInterface;
15
use LoginCidadao\OAuthBundle\Model\ClientInterface;
16
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;
17
use LoginCidadao\OAuthBundle\Validator\Constraints\DomainOwnership;
18
use LoginCidadao\OAuthBundle\Validator\Constraints\SectorIdentifier;
19
use Symfony\Component\Validator\Constraints as Assert;
20
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
21
22
/**
23
 * @ORM\Entity(repositoryClass="LoginCidadao\OAuthBundle\Entity\OrganizationRepository")
24
 * @ORM\Table(name="organization")
25
 * @ORM\HasLifecycleCallbacks()
26
 * @UniqueEntity("domain")
27
 * @UniqueEntity("name")
28
 * @DomainOwnership
29
 * @SectorIdentifier
30
 */
31
class Organization implements OrganizationInterface
32
{
33
    /**
34
     * @ORM\Id
35
     * @ORM\Column(type="integer")
36
     * @ORM\GeneratedValue(strategy="AUTO")
37
     */
38
    private $id;
39
40
    /**
41
     * @ORM\Column(name="name", type="string", nullable=false, unique=true)
42
     * @Assert\NotBlank
43
     * @var string
44
     */
45
    private $name;
46
47
    /**
48
     * @var PersonInterface[]
49
     * @ORM\ManyToMany(targetEntity="LoginCidadao\CoreBundle\Model\PersonInterface")
50
     * @ORM\JoinTable(name="person_organizations",
51
     *      joinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id")},
52
     *      inverseJoinColumns={@ORM\JoinColumn(name="person_id", referencedColumnName="id")}
53
     * )
54
     */
55
    private $members;
56
57
    /**
58
     * @ORM\Column(name="verified_at", type="datetime", nullable=true)
59
     * @var \DateTime
60
     */
61
    private $verifiedAt;
62
63
    /**
64
     * @ORM\Column(name="domain", type="string", nullable=false, unique=true)
65
     * @var string
66
     */
67
    private $domain;
68
69
    /**
70
     * @ORM\OneToMany(targetEntity="LoginCidadao\OpenIDBundle\Entity\ClientMetadata", mappedBy="organization")
71
     * @var ClientInterface[]
72
     */
73
    private $clients;
74
75
    /**
76
     * @Assert\Url
77
     * @ORM\Column(name="validation_url", type="string", nullable=true, unique=true)
78
     * @var string
79
     */
80
    private $validationUrl;
81
82
    /**
83
     * @ORM\Column(name="validation_secret", type="string", nullable=true)
84
     * @var string
85
     */
86
    private $validationSecret;
87
88
    /**
89
     * @ORM\Column(name="validated_url", type="string", nullable=true)
90
     * @var string
91
     */
92
    private $validatedUrl;
93
94
    /**
95
     * @ORM\Column(name="trusted", type="boolean", nullable=false)
96
     * @var boolean
97
     */
98
    private $trusted;
99
100
    /**
101
     * @Assert\Url
102
     * @ORM\Column(name="sector_identifier_uri", type="string", nullable=true, unique=true)
103
     * @var string
104
     */
105
    private $sectorIdentifierUri;
106
107 3
    public function __construct()
108
    {
109 3
        $this->members = [];
110 3
        $this->clients = [];
111 3
        $this->initializeValidationCode();
112 3
        $this->trusted = false;
113 3
    }
114
115 1
    public function getId()
116
    {
117 1
        return $this->id;
118
    }
119
120 1
    public function setId($id)
121
    {
122 1
        $this->id = $id;
123
124 1
        return $this;
125
    }
126
127 1
    public function getName()
128
    {
129 1
        return $this->name;
130
    }
131
132 1
    public function setName($name)
133
    {
134 1
        $this->name = $name;
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @return PersonInterface[]
141
     */
142 1
    public function getMembers()
143
    {
144 1
        return $this->members;
145
    }
146
147
    /**
148
     * @param PersonInterface[] $members
149
     * @return OrganizationInterface
150
     */
151 1
    public function setMembers(array $members)
152
    {
153 1
        $this->members = $members;
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * @return \DateTime
160
     */
161 1
    public function getVerifiedAt()
162
    {
163 1
        return $this->verifiedAt;
164
    }
165
166
    /**
167
     * @param \DateTime $verifiedAt
168
     * @return OrganizationInterface
169
     */
170 1
    public function setVerifiedAt($verifiedAt)
171
    {
172 1
        $this->verifiedAt = $verifiedAt;
173
174 1
        return $this;
175
    }
176
177 1
    public function getDomain()
178
    {
179 1
        return $this->domain;
180
    }
181
182 1
    public function setDomain($domain)
183
    {
184 1
        $this->domain = $domain;
185
186 1
        return $this;
187
    }
188
189 1
    public function getClients()
190
    {
191 1
        return $this->clients;
192
    }
193
194 1
    public function setClients(array $clients)
195
    {
196 1
        $this->clients = $clients;
197
198 1
        return $this;
199
    }
200
201 1
    public function getValidationUrl()
202
    {
203 1
        return $this->validationUrl;
204
    }
205
206 1
    public function setValidationUrl($validationUrl)
207
    {
208 1
        $this->validationUrl = $validationUrl;
209
210 1
        return $this;
211
    }
212
213 1
    public function getValidationSecret()
214
    {
215 1
        $this->initializeValidationCode();
216
217 1
        return $this->validationSecret;
218
    }
219
220 3
    public function setValidationSecret($validationSecret)
221
    {
222 3
        $this->validationSecret = $validationSecret;
223
224 3
        return $this;
225
    }
226
227 1
    public function checkValidation()
228
    {
229 1
        if ($this->validatedUrl !== $this->getValidationUrl()) {
230 1
            $this->setVerifiedAt(null);
231 1
            $this->validatedUrl = null;
232
233 1
            return false;
234
        }
235
236 1
        return true;
237
    }
238
239 1
    public function setValidatedUrl($validatedUrl)
240
    {
241 1
        $this->validatedUrl = $validatedUrl;
242
243 1
        return $this;
244
    }
245
246 1
    public function __toString()
247
    {
248 1
        return $this->getName();
249
    }
250
251 1
    public function isVerified()
252
    {
253 1
        return $this->getVerifiedAt() instanceof \DateTime;
254
    }
255
256 1
    public function isTrusted()
257
    {
258 1
        return $this->trusted;
259
    }
260
261
    /**
262
     *
263
     * @param boolean $trusted
264
     * @return OrganizationInterface
265
     */
266 1
    public function setTrusted($trusted)
267
    {
268 1
        $this->trusted = $trusted;
269
270 1
        return $this;
271
    }
272
273
    /**
274
     * @return string
275
     */
276 1
    public function getSectorIdentifierUri()
277
    {
278 1
        return $this->sectorIdentifierUri;
279
    }
280
281
    /**
282
     * @param string $sectorIdentifierUri
283
     * @return Organization
284
     */
285 1
    public function setSectorIdentifierUri($sectorIdentifierUri)
286
    {
287 1
        $this->sectorIdentifierUri = $sectorIdentifierUri;
288
289 1
        return $this;
290
    }
291
292
    /**
293
     * @ORM\PrePersist
294
     * @ORM\PreUpdate
295
     */
296 3
    private function initializeValidationCode()
297
    {
298 3
        if ($this->validationSecret) {
299 1
            return;
300
        }
301 3
        $random = base64_encode(random_bytes(35));
302 3
        $this->setValidationSecret($random);
303 3
    }
304
305
306
}
307