A gateway/firewall task to be able to talk someone about the real job
Fibinger Ádám
2021-10-22 fd7692eae00cbb0db3e6b732f68357e3c64a8a8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
 
namespace App\DataTransformer;
 
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\Validator\ValidatorInterface;
use App\Dto\CreateSecretDTO;
use App\Entity\Secret;
 
class CreateSecretTransformer implements DataTransformerInterface
{
    /**
     * @var ValidatorInterface
     */
    private $validator;
 
    public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }
 
    /**
     * @inheritDoc
     */
    public function transform($object, string $to, array $context = [])
    {
        $this->validator->validate($object);
 
        $expireInMinutes = $object->expireAfter > 0 ? $object->expireAfter : 0;
 
        $expireTime = null;
 
        if ($expireInMinutes)
        {
            $expireTime = (new \DateTimeImmutable())
                ->add(new \DateInterval('PT' . $expireInMinutes . 'M'));
        }
 
        return (new Secret())
            ->setSecretText($object->secret)
            ->setExpiresAt($expireTime)
            ->setRemainingViews($object->expireAfterViews);
    }
 
    /**
     * @inheritDoc
     */
    public function supportsTransformation($data, string $to, array $context = []): bool
    {
        if ($to !== Secret::class)
        {
            return false;
        }
 
        if ($data instanceof CreateSecretDTO)
        {
            return true;
        }
 
        if (isset($data['secret']) && isset($data["expireAfterViews"]))
        {
            return true;
        }
 
        return false;
    }
}