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

systemsdk / docker-symfony-api / #75

pending completion
#75

push

DKravtsov
Fixed issue with REDIS volumes, updated dependencies, refactoring

39 of 39 new or added lines in 12 files covered. (100.0%)

1470 of 2656 relevant lines covered (55.35%)

23.64 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_merge;
30
use function array_unique;
31
use function array_values;
32

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

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

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

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

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

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

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

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

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

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

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

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

150
        return $this;
6✔
151
    }
152

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

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

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

170
        return $this;
11✔
171
    }
172

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

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

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

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

228
        return $this;
7✔
229
    }
230

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

240
        return $this;
×
241
    }
242

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

250
        return $this;
8✔
251
    }
252
}
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

© 2025 Coveralls, Inc