• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #85

pending completion
#85

push

DKravtsov
Updated dependencies, refactoring.

21 of 21 new or added lines in 14 files covered. (100.0%)

1440 of 2683 relevant lines covered (53.67%)

21.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.78
/src/ApiKey/Domain/Entity/ApiKey.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\ApiKey\Domain\Entity;
6

7
use App\General\Domain\Entity\Interfaces\EntityInterface;
8
use App\General\Domain\Entity\Traits\Timestampable;
9
use App\General\Domain\Entity\Traits\Uuid;
10
use App\Log\Domain\Entity\LogRequest;
11
use App\Role\Domain\Enum\Role as RoleEnum;
12
use App\User\Domain\Entity\Interfaces\UserGroupAwareInterface;
13
use App\User\Domain\Entity\Traits\Blameable;
14
use App\User\Domain\Entity\UserGroup;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\DBAL\Types\Types;
18
use Doctrine\ORM\Mapping as ORM;
19
use OpenApi\Annotations as OA;
20
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
21
use Ramsey\Uuid\UuidInterface;
22
use Symfony\Bridge\Doctrine\Validator\Constraints as AssertCollection;
23
use Symfony\Component\Serializer\Annotation\Groups;
24
use Symfony\Component\String\ByteString;
25
use Symfony\Component\Validator\Constraints as Assert;
26
use Throwable;
27

28
use function array_map;
29
use function array_unique;
30
use function array_values;
31

32
/**
33
 * Class ApiKey
34
 *
35
 * @package App\ApiKey
36
 */
37
#[ORM\Entity]
38
#[ORM\Table(name: 'api_key')]
39
#[ORM\UniqueConstraint(
40
    name: 'uq_token',
41
    columns: ['token'],
42
)]
43
#[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
44
#[AssertCollection\UniqueEntity('token')]
45
class ApiKey implements EntityInterface, UserGroupAwareInterface
46
{
47
    use Blameable;
48
    use Timestampable;
49
    use Uuid;
50

51
    /**
52
     * @OA\Property(type="string", format="uuid")
53
     */
54
    #[ORM\Id]
55
    #[ORM\Column(
56
        name: 'id',
57
        type: UuidBinaryOrderedTimeType::NAME,
58
        unique: true,
59
    )]
60
    #[Groups([
61
        'ApiKey',
62
        'ApiKey.id',
63

64
        'LogRequest.apiKey',
65
    ])]
66
    private UuidInterface $id;
67

68
    #[ORM\Column(
69
        name: 'token',
70
        type: Types::STRING,
71
        length: 40,
72
        options: [
73
            'comment' => 'Generated API key string for authentication',
74
        ],
75
    )]
76
    #[Groups([
77
        'ApiKey',
78
        'ApiKey.token',
79
    ])]
80
    #[Assert\NotBlank]
81
    #[Assert\NotNull]
82
    #[Assert\Length(exactly: 40)]
83
    private string $token = '';
84

85
    #[ORM\Column(
86
        name: 'description',
87
        type: Types::TEXT,
88
    )]
89
    #[Groups([
90
        'ApiKey',
91
        'ApiKey.description',
92
    ])]
93
    #[Assert\NotNull]
94
    private string $description = '';
95

96
    /**
97
     * @var Collection<int, UserGroup>|ArrayCollection<int, UserGroup>
98
     */
99
    #[ORM\JoinTable(name: 'api_key_has_user_group')]
100
    #[ORM\ManyToMany(
101
        targetEntity: UserGroup::class,
102
        inversedBy: 'apiKeys',
103
    )]
104
    #[Groups([
105
        'ApiKey.userGroups',
106
    ])]
107
    private Collection | ArrayCollection $userGroups;
108

109
    /**
110
     * @var Collection<int, LogRequest>|ArrayCollection<int, LogRequest>
111
     */
112
    #[ORM\OneToMany(
113
        mappedBy: 'apiKey',
114
        targetEntity: LogRequest::class,
115
    )]
116
    #[Groups([
117
        'ApiKey.logsRequest',
118
    ])]
119
    private Collection | ArrayCollection $logsRequest;
120

121
    /**
122
     * Constructor
123
     *
124
     * @throws Throwable
125
     */
126
    public function __construct()
127
    {
128
        $this->id = $this->createUuid();
6✔
129
        $this->userGroups = new ArrayCollection();
6✔
130
        $this->logsRequest = new ArrayCollection();
6✔
131

132
        $this->generateToken();
6✔
133
    }
134

135
    public function getId(): string
136
    {
137
        return $this->id->toString();
20✔
138
    }
139

140
    public function getToken(): string
141
    {
142
        return $this->token;
15✔
143
    }
144

145
    public function setToken(string $token): self
146
    {
147
        $this->token = $token;
6✔
148

149
        return $this;
6✔
150
    }
151

152
    /**
153
     * @throws Throwable
154
     */
155
    public function generateToken(): self
156
    {
157
        return $this->setToken(ByteString::fromRandom(40)->toString());
6✔
158
    }
159

160
    public function getDescription(): string
161
    {
162
        return $this->description;
15✔
163
    }
164

165
    public function setDescription(string $description): self
166
    {
167
        $this->description = $description;
11✔
168

169
        return $this;
11✔
170
    }
171

172
    /**
173
     * Getter method for user groups collection.
174
     *
175
     * @return Collection<int, UserGroup>|ArrayCollection<int, UserGroup>
176
     */
177
    public function getUserGroups(): Collection | ArrayCollection
178
    {
179
        return $this->userGroups;
5✔
180
    }
181

182
    /**
183
     * Getter method for user request log collection.
184
     *
185
     * @return Collection<int, LogRequest>|ArrayCollection<int, LogRequest>
186
     */
187
    public function getLogsRequest(): Collection | ArrayCollection
188
    {
189
        return $this->logsRequest;
×
190
    }
191

192
    /**
193
     * Getter for roles.
194
     *
195
     * @return array<int, string>
196
     */
197
    #[Groups([
198
        'ApiKey.roles',
199
    ])]
200
    public function getRoles(): array
201
    {
202
        return array_values(
×
203
            array_map(
×
204
                '\strval',
×
205
                array_unique(
×
206
                    [
×
207
                        RoleEnum::API->value,
×
208
                        ...$this->userGroups
×
209
                            ->map(static fn (UserGroup $userGroup): string => $userGroup->getRole()->getId())
×
210
                            ->toArray(),
×
211
                    ],
×
212
                ),
×
213
            ),
×
214
        );
×
215
    }
216

217
    /**
218
     * Method to attach new userGroup to current api key.
219
     */
220
    public function addUserGroup(UserGroup $userGroup): self
221
    {
222
        if ($this->userGroups->contains($userGroup) === false) {
10✔
223
            $this->userGroups->add($userGroup);
9✔
224
            $userGroup->addApiKey($this);
9✔
225
        }
226

227
        return $this;
7✔
228
    }
229

230
    /**
231
     * Method to remove specified userGroup from current api key.
232
     */
233
    public function removeUserGroup(UserGroup $userGroup): self
234
    {
235
        if ($this->userGroups->removeElement($userGroup)) {
×
236
            $userGroup->removeApiKey($this);
×
237
        }
238

239
        return $this;
×
240
    }
241

242
    /**
243
     * Method to remove all many-to-many userGroup relations from current api key.
244
     */
245
    public function clearUserGroups(): self
246
    {
247
        $this->userGroups->clear();
8✔
248

249
        return $this;
8✔
250
    }
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc