Passed
Push — main ( f1aa52...bbae52 )
by Karl
06:11
created

User::addOrganization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Entity;
4
5
use App\Repository\UserRepository;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
10
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
#[ORM\Entity(repositoryClass: UserRepository::class)]
14
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
15
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
16
class User implements UserInterface, PasswordAuthenticatedUserInterface
17
{
18
    #[ORM\Id]
19
    #[ORM\GeneratedValue]
20
    #[ORM\Column]
21
    private ?int $id = null;
22
23
    #[ORM\Column(length: 180)]
24
    private ?string $email = null;
25
26
    /**
27
     * @var list<string> The user roles
0 ignored issues
show
Bug introduced by
The type App\Entity\list 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...
28
     */
29
    #[ORM\Column]
30
    private array $roles = [];
31
32
    /**
33
     * @var string The hashed password
34
     */
35
    #[ORM\Column]
36
    private ?string $password = null;
37
38
    /**
39
     * @var Collection<int, Organization>
40
     */
41
    #[ORM\ManyToMany(targetEntity: Organization::class, mappedBy: 'User')]
42
    private Collection $organizations;
43
44
    public function __construct()
45
    {
46
        $this->organizations = new ArrayCollection();
47
    }
48
49
    public function getId(): ?int
50
    {
51
        return $this->id;
52
    }
53
54
    public function getEmail(): ?string
55
    {
56
        return $this->email;
57
    }
58
59
    public function setEmail(string $email): static
60
    {
61
        $this->email = $email;
62
63
        return $this;
64
    }
65
66
    /**
67
     * A visual identifier that represents this user.
68
     *
69
     * @see UserInterface
70
     */
71
    public function getUserIdentifier(): string
72
    {
73
        return (string) $this->email;
74
    }
75
76
    /**
77
     * @see UserInterface
78
     *
79
     * @return string[]
80
     */
81
    public function getRoles(): array
82
    {
83
        $roles = $this->roles;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->roles of type array is incompatible with the declared type App\Entity\list of property $roles.

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...
84
        // guarantee every user at least has ROLE_USER
85
        $roles[] = 'ROLE_USER';
86
87
        return array_unique($roles);
88
    }
89
90
    /**
91
     * @param list<string> $roles
92
     */
93
    public function setRoles(array $roles): static
94
    {
95
        $this->roles = $roles;
0 ignored issues
show
Documentation Bug introduced by
It seems like $roles of type array is incompatible with the declared type App\Entity\list of property $roles.

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...
96
97
        return $this;
98
    }
99
100
    /**
101
     * @see PasswordAuthenticatedUserInterface
102
     */
103
    public function getPassword(): string
104
    {
105
        return $this->password;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->password could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108
    public function setPassword(string $password): static
109
    {
110
        $this->password = $password;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @see UserInterface
117
     */
118
    public function eraseCredentials(): void
119
    {
120
        // If you store any temporary, sensitive data on the user, clear it here
121
        // $this->plainPassword = null;
122
    }
123
124
    /**
125
     * @return Collection<int, Organization>
126
     */
127
    public function getOrganizations(): Collection
128
    {
129
        return $this->organizations;
130
    }
131
132
    public function addOrganization(Organization $organization): static
133
    {
134
        if (!$this->organizations->contains($organization)) {
135
            $this->organizations->add($organization);
136
            $organization->addUser($this);
137
        }
138
139
        return $this;
140
    }
141
142
    public function removeOrganization(Organization $organization): static
143
    {
144
        if ($this->organizations->removeElement($organization)) {
145
            $organization->removeUser($this);
146
        }
147
148
        return $this;
149
    }
150
}
151