Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.88% covered (warning)
87.88%
29 / 33
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
DecryptedAuthenticatedUser
87.88% covered (warning)
87.88%
29 / 33
66.67% covered (warning)
66.67%
6 / 9
11.22
0.00% covered (danger)
0.00%
0 / 1
 createFromEntity
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getIdentifierClassName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEntityClassName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isExpired
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExpireTime
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBoundedContextId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 refresh
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 convert
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
3.02
1<?php
2namespace Apie\Common\ValueObjects;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\Entities\EntityInterface;
6use Apie\Core\Identifiers\IdentifierInterface;
7use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
8use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
9use Apie\Core\ValueObjects\IsStringValueObject;
10use ReflectionClass;
11use ReflectionException;
12
13/**
14 * @template T of EntityInterface
15 */
16final class DecryptedAuthenticatedUser implements StringValueObjectInterface
17{
18    use IsStringValueObject;
19
20    /** @var class-string<IdentifierInterface<T>> */
21    private string $className;
22
23    private BoundedContextId $boundedContextId;
24
25    /** @var IdentifierInterface<T> */
26    private IdentifierInterface $id;
27
28    private int $expireTime;
29
30    /**
31     * @template U of EntityInterface
32     * @param U $entity
33     * @return DecryptedAuthenticatedUser<U>
34     */
35    public static function createFromEntity(
36        EntityInterface $entity,
37        BoundedContextId $boundedContextId,
38        int $time
39    ): self {
40        return new self(
41            get_class($entity->getId())
42            . '/'
43            . $boundedContextId
44            . '/'
45            . $entity->getId()
46            . '/'
47            . $time
48        );
49    }
50
51    /**
52     * @return class-string<IdentifierInterface<T>>
53     */
54    public function getIdentifierClassName(): string
55    {
56        return $this->className;
57    }
58
59    /**
60     * @return class-string<T>
61     */
62    public function getEntityClassName(): string
63    {
64        return $this->className::getReferenceFor()->name;
65    }
66
67    public function isExpired(): bool
68    {
69        return $this->expireTime <= time();
70    }
71
72    public function getExpireTime(): int
73    {
74        return $this->expireTime;
75    }
76
77    public function getBoundedContextId(): BoundedContextId
78    {
79        return $this->boundedContextId;
80    }
81
82    /**
83     * @return IdentifierInterface<T>
84     */
85    public function getId(): IdentifierInterface
86    {
87        return $this->id;
88    }
89
90    /**
91     * @return DecryptedAuthenticatedUser<T>
92     */
93    public function refresh(int $expireTime): self
94    {
95        $res = clone $this;
96        $res->expireTime = $expireTime;
97        return $res;
98    }
99
100    protected function convert(string $input): string
101    {
102        list($className, $boundedContextId, $id, $expireTime) = explode(
103            '/',
104            $input,
105            4
106        );
107        try {
108            $refl = new ReflectionClass($className);
109            if (!in_array(IdentifierInterface::class, $refl->getInterfaceNames(), true)) {
110                throw new InvalidStringForValueObjectException($input, $this);
111            }
112        } catch (ReflectionException $previous) {
113            throw new InvalidStringForValueObjectException($input, $this, $previous);
114        }
115        $this->className = $className;
116        $this->boundedContextId = new BoundedContextId($boundedContextId);
117        $this->id = new $className($id);
118        $this->expireTime = (int) $expireTime;
119
120        return $input;
121    }
122}