Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.62% covered (success)
90.62%
29 / 32
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
DecryptedAuthenticatedUser
90.62% covered (success)
90.62%
29 / 32
75.00% covered (warning)
75.00%
6 / 8
10.08
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
 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    public function isExpired(): bool
60    {
61        return $this->expireTime <= time();
62    }
63
64    public function getExpireTime(): int
65    {
66        return $this->expireTime;
67    }
68
69    public function getBoundedContextId(): BoundedContextId
70    {
71        return $this->boundedContextId;
72    }
73
74    /**
75     * @return IdentifierInterface<T>
76     */
77    public function getId(): IdentifierInterface
78    {
79        return $this->id;
80    }
81
82    /**
83     * @return DecryptedAuthenticatedUser<T>
84     */
85    public function refresh(int $expireTime): self
86    {
87        $res = clone $this;
88        $res->expireTime = $expireTime;
89        return $res;
90    }
91
92    protected function convert(string $input): string
93    {
94        list($className, $boundedContextId, $id, $expireTime) = explode(
95            '/',
96            $input,
97            4
98        );
99        try {
100            $refl = new ReflectionClass($className);
101            if (!in_array(IdentifierInterface::class, $refl->getInterfaceNames(), true)) {
102                throw new InvalidStringForValueObjectException($input, $this);
103            }
104        } catch (ReflectionException $previous) {
105            throw new InvalidStringForValueObjectException($input, $this, $previous);
106        }
107        $this->className = $className;
108        $this->boundedContextId = new BoundedContextId($boundedContextId);
109        $this->id = new $className($id);
110        $this->expireTime = (int) $expireTime;
111
112        return $input;
113    }
114}