Organization::getJournals()   A
last analyzed

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\OneToMany(targetEntity: Journal::class, mappedBy: 'organization')]
48
    private Collection $journals;
49
50
    public function __construct()
51
    {
52
        $this->users = new ArrayCollection();
53
        $this->journals = new ArrayCollection();
54
    }
55
56
    public function getId(): ?int
57
    {
58
        return $this->id;
59
    }
60
61
    public function getName(): ?string
62
    {
63
        return $this->name;
64
    }
65
66
    public function setName(string $name): static
67
    {
68
        $this->name = $name;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return Collection<int, User>
75
     */
76
    public function getUsers(): Collection
77
    {
78
        return $this->users;
79
    }
80
81
    public function isUserInOrganization(User $user): bool
82
    {
83
        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

83
        if (!$this->users->/** @scrutinizer ignore-call */ isInitialized()) {
Loading history...
84
            $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

84
            $this->users->/** @scrutinizer ignore-call */ 
85
                          initialize();
Loading history...
85
        }
86
        dump($this->users);
87
        dump($this->users->toArray());
88
        dump($user);
89
        return $this->users->contains($user);
90
    }
91
92
    public function addUser(User $user): static
93
    {
94
        if (!$this->users->contains($user)) {
95
            $this->users->add($user);
96
        }
97
98
        return $this;
99
    }
100
101
    public function removeUser(User $user): static
102
    {
103
        $this->users->removeElement($user);
104
105
        return $this;
106
    }
107
108
    public function getJournals(): Collection
109
    {
110
        return $this->journals;
111
    }
112
113
    public function addJournal(Journal $journal): static
114
    {
115
        if (!$this->journals->contains($journal)) {
116
            $this->journals->add($journal);
117
        }
118
119
        return $this;
120
    }
121
122
    public function removeJournal(Journal $journal): static
123
    {
124
        $this->journals->removeElement($journal);
125
126
        return $this;
127
    }
128
129
    // public function getJournals(): ?Journal
130
    // {
131
    //     return $this->journal;
132
    // }
133
134
    // public function setJournals(?Journal $journal): static
135
    // {
136
    //     $this->journal = $journal;
137
138
    //     return $this;
139
    // }
140
}
141