for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types = 1);
/**
* /src/Security/ApiKeyUser.php
*
* @author TLe, Tarmo Leppänen <[email protected]>
*/
namespace App\Security;
use App\Entity\ApiKey;
use App\Security\Interfaces\ApiKeyUserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use function array_merge;
use function array_unique;
* Class ApiKeyUser
* @package App\Security
class ApiKeyUser implements ApiKeyUserInterface
{
* @Groups({
* "ApiKeyUser",
* "ApiKeyUser.apiKey",
* })
private string $username;
private ApiKey $apiKey;
* @var array<int, string>
* "ApiKeyUser.roles",
private array $roles;
* {@inheritdoc}
public function __construct(ApiKey $apiKey, array $roles)
$this->apiKey = $apiKey;
$this->username = $this->apiKey->getToken();
$this->roles = array_unique(array_merge($roles, [RolesService::ROLE_API]));
}
public function getApiKey(): ApiKey
return $this->apiKey;
* @return array<int, string> The user roles
public function getRoles(): array
return $this->roles;
* @codeCoverageIgnore
public function getPassword(): string
return '';
public function getSalt(): ?string
return null;
public function getUserIdentifier(): string
return $this->username;
public function eraseCredentials(): void
* @todo Remove this when this `getUsername` is removed from main interface (Symfony 6.0)
public function getUsername(): string
return $this->getUserIdentifier();