Passed
Push — master ( c40c76...28a572 )
by Loïc
09:36 queued 11s
created

supportsTransformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\DataTransformer;
6
7
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
8
use App\Message\AppUserIdAwareInterface;
9
use Monofony\Contracts\Core\Model\User\AppUserInterface;
10
use Sylius\Component\User\Model\UserInterface;
11
use Symfony\Component\Security\Core\Security;
12
13
final class LoggedInAppUserIdAwareDataTransformer implements DataTransformerInterface
14
{
15
    private $security;
16
17
    public function __construct(Security $security)
18
    {
19
        $this->security = $security;
20
    }
21
22
    /**
23
     * @param AppUserIdAwareInterface $object
24
     * @psalm-suppress MoreSpecificImplementedParamType
25
     */
26
    public function transform($object, string $to, array $context = []): AppUserIdAwareInterface
27
    {
28
        /** @var AppUserInterface|UserInterface $user */
29
        $user = $this->security->getUser();
30
31
        if (!$user instanceof AppUserInterface) {
32
            return $object;
33
        }
34
35
        $object->setAppUserId($user->getId());
36
37
        return $object;
38
    }
39
40
    public function supportsTransformation($data, string $to, array $context = []): bool
41
    {
42
        return is_a($context['input']['class'], AppUserIdAwareInterface::class, true);
43
    }
44
}
45