Passed
Push — main ( f0ecf6...ccc364 )
by Karl
25:43
created

Organization::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Entity;
4
5
use ApiPlatform\Metadata\ApiResource;
6
use ApiPlatform\Metadata\Delete;
7
use ApiPlatform\Metadata\Get;
8
use ApiPlatform\Metadata\GetCollection;
9
use ApiPlatform\Metadata\Post;
10
use ApiPlatform\Metadata\Put;
11
use App\Repository\OrganizationRepository;
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Doctrine\Common\Collections\Collection;
14
use Doctrine\ORM\Mapping as ORM;
15
16
#[ORM\Entity(repositoryClass: OrganizationRepository::class)]
17
#[ApiResource(
18
    description: 'The Organization entity represents a business or group that
19
     uses the bookkeeping application. An organization typically corresponds 
20
     to a company, non-profit, or other type of entity that manages financial
21
      records within the app. Each organization can be owned and managed by
22
       one or more users.',
23
    operations: [
24
        new Get(security: 'object.isUserInOrganization(user)'),
25
        new Post(security: 'is_granted("ROLE_USER")'),
26
        new Put(),
27
        new Delete(security: 'object.isUserInOrganization(user)'),
28
        new GetCollection(),
29
    ]
30
)]
31
class Organization
32
{
33
    #[ORM\Id]
34
    #[ORM\GeneratedValue]
35
    #[ORM\Column]
36
    private ?int $id = null;
37
38
    #[ORM\Column(length: 255)]
39
    private ?string $name = null;
40
41
    /**
42
     * @var Collection<int, User>
43
     */
44
    #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'organizations')]
45
    private Collection $users;
46
47
    #[ORM\ManyToOne(inversedBy: 'organization')]
48
    private ?Journal $journal = null;
49
50
    public function __construct()
51
    {
52
        $this->users = new ArrayCollection();
53
    }
54
55
    public function getId(): ?int
56
    {
57
        return $this->id;
58
    }
59
60
    public function getName(): ?string
61
    {
62
        return $this->name;
63
    }
64
65
    public function setName(string $name): static
66
    {
67
        $this->name = $name;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return Collection<int, User>
74
     */
75
    public function getUsers(): Collection
76
    {
77
        return $this->users;
78
    }
79
80
    public function isUserInOrganization(User $user): bool
81
    {
82
        if (!$this->users->isInitialized()) {
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Doctrine\Common\Collections\Collection. It seems like you code against a sub-type of Doctrine\Common\Collections\Collection such as Doctrine\Common\Collections\AbstractLazyCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        if (!$this->users->/** @scrutinizer ignore-call */ isInitialized()) {
Loading history...
83
            $this->users->initialize();
0 ignored issues
show
Bug introduced by
The method initialize() does not exist on Doctrine\Common\Collections\Collection. It seems like you code against a sub-type of Doctrine\Common\Collections\Collection such as Doctrine\Common\Collections\AbstractLazyCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            $this->users->/** @scrutinizer ignore-call */ 
84
                          initialize();
Loading history...
84
        }
85
        dump($this->users);
86
        dump($this->users->toArray());
87
        dump($user);
88
        return $this->users->contains($user);
89
    }
90
91
    public function addUser(User $user): static
92
    {
93
        if (!$this->users->contains($user)) {
94
            $this->users->add($user);
95
        }
96
97
        return $this;
98
    }
99
100
    public function removeUser(User $user): static
101
    {
102
        $this->users->removeElement($user);
103
104
        return $this;
105
    }
106
107
    public function getJournal(): ?Journal
108
    {
109
        return $this->journal;
110
    }
111
112
    public function setJournal(?Journal $journal): static
113
    {
114
        $this->journal = $journal;
115
116
        return $this;
117
    }
118
}
119