Passed
Push — master ( ece3c1...a0e7b2 )
by Florian
02:27
created

User::getRegistrationFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Entity;
13
14
use App\Entity\Base\BaseEntity;
15
use App\Entity\Traits\ContactInformationTrait;
16
use App\Entity\Traits\IdTrait;
17
use App\Entity\Traits\TimeTrait;
18
use App\Helper\HashHelper;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\ORM\Mapping as ORM;
21
22
/**
23
 * @ORM\Entity()
24
 * @ORM\HasLifecycleCallbacks
25
 */
26
class User extends BaseEntity
27
{
28
    use IdTrait;
29
    use TimeTrait;
30
    use ContactInformationTrait;
31
32
    // can use any features & impersonate users
33
    const ROLE_ADMIN = 'ROLE_ADMIN';
34
35
    // can use any features
36
    const ROLE_USER = 'ROLE_USER';
37
38
    /**
39
     * @var string|null
40
     *
41
     * @ORM\Column(type="text", nullable=true)
42
     */
43
    private $authenticationHash;
44
45
    /**
46
     * @var bool
47
     *
48
     * @ORM\Column(type="boolean", options={"default": false})
49
     */
50
    private $isAdminAccount = false;
51
52
    /**
53
     * @var Registration[]|ArrayCollection
54
     *
55
     * @ORM\OneToMany (targetEntity="App\Entity\Registration", mappedBy="user")
56
     */
57
    private $registrations;
58
59
    /**
60
     * constructor.
61
     */
62
    public function __construct()
63
    {
64
        $this->registrations = new ArrayCollection();
65
    }
66
67
    public static function createFromRegistration(Registration $registration)
68
    {
69
        $user = new User();
70
        $user->fromOtherContactInformation($registration);
71
72
        return $user;
73
    }
74
75
    /**
76
     * @return Registration[]|ArrayCollection
77
     */
78
    public function getRegistrations()
79
    {
80
        return $this->registrations;
81
    }
82
83
    public function generateAuthenticationHash()
84
    {
85
        $this->authenticationHash = HashHelper::getHash();
86
    }
87
88
    /**
89
     * Returns the roles granted to the user.
90
     *
91
     * <code>
92
     * public function getRoles()
93
     * {
94
     *     return array('ROLE_USER');
95
     * }
96
     * </code>
97
     *
98
     * Alternatively, the roles might be stored on a ``roles`` property,
99
     * and populated in any number of different ways when the user object
100
     * is created.
101
     *
102
     * @return string[] The user roles
103
     */
104
    public function getRoles()
105
    {
106
        if ($this->isAdminAccount) {
107
            return [self::ROLE_ADMIN];
108
        }
109
110
        return [self::ROLE_USER];
111
    }
112
113
    public function getAuthenticationHash(): ?string
114
    {
115
        return $this->authenticationHash;
116
    }
117
118
    public function getRegistrationFor(Event $event): ?Registration
119
    {
120
        foreach ($this->getRegistrations() as $registration) {
121
            if ($registration->getEvent() === $event) {
122
                return $registration;
123
            }
124
        }
125
126
        return null;
127
    }
128
}
129