Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SymfonyUserDecoratorIdentifier
81.82% covered (warning)
81.82%
9 / 11
50.00% covered (danger)
50.00%
2 / 4
5.15
0.00% covered (danger)
0.00%
0 / 1
 createFrom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convert
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 createRandom
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getReferenceFor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Apie\ApieBundle\Security;
4
5use Apie\Core\Attributes\FakeMethod;
6use Apie\Core\Identifiers\IdentifierInterface;
7use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
8use Apie\Core\ValueObjects\IsStringValueObject;
9use ReflectionClass;
10use Symfony\Component\Security\Core\User\InMemoryUser;
11use Symfony\Component\Security\Core\User\UserInterface;
12
13/**
14 * @implements IdentifierInterface<SymfonyUserDecorator>
15 */
16#[FakeMethod('createRandom')]
17final class SymfonyUserDecoratorIdentifier implements IdentifierInterface
18{
19    use IsStringValueObject;
20
21    /**
22     * @var class-string<UserInterface>
23     */
24    private string $userClass;
25
26    private string $userId;
27
28    public static function createFrom(UserInterface $user): self
29    {
30        return new self(get_class($user) . '@' . $user->getUserIdentifier());
31    }
32
33    protected function convert(string $input): string
34    {
35        $split = explode('@', $input, 2);
36        if (!class_exists($split[0])) {
37            throw new InvalidStringForValueObjectException($input, $this);
38        }
39        $this->userClass = $split[0];
40        $this->userId = $split[1];
41        return $this->userClass . '@' . $this->userId;
42    }
43
44    public static function createRandom(): self
45    {
46        return self::createFrom(
47            new InMemoryUser('username' . random_int(0, 100), 'password', ['ROLE_USER'])
48        );
49    }
50
51    public static function getReferenceFor(): ReflectionClass
52    {
53        return new ReflectionClass(SymfonyUserDecorator::class);
54    }
55}