Completed
Push — master ( 3ef484...ea5778 )
by Paweł
08:46
created

TenantAwareRouteToIdTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transform() 0 12 3
B reverseTransform() 0 24 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Form\DataTransformer;
18
19
use SWP\Bundle\ContentBundle\Model\RouteInterface;
20
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
21
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
22
use Symfony\Component\Form\DataTransformerInterface;
23
use Symfony\Component\Form\Exception\TransformationFailedException;
24
use Symfony\Component\Form\Exception\UnexpectedTypeException;
25
26
final class TenantAwareRouteToIdTransformer implements DataTransformerInterface
27
{
28
    /**
29
     * @var TenantContextInterface
30
     */
31
    private $tenantContext;
32
33
    /**
34
     * @var RouteRepositoryInterface
35
     */
36
    private $routeRepository;
37
38
    /**
39
     * TenantAwareRouteToIdTransformer constructor.
40
     *
41
     * @param TenantContextInterface   $tenantContext
42
     * @param RouteRepositoryInterface $routeRepository
43
     */
44
    public function __construct(TenantContextInterface $tenantContext, RouteRepositoryInterface $routeRepository)
45
    {
46
        $this->tenantContext = $tenantContext;
47
        $this->routeRepository = $routeRepository;
48
    }
49
50
    /**
51
     * Transforms an object (route) to a string (id).
52
     *
53
     * @param RouteInterface|string $route
54
     *
55
     * @return string
56
     *
57
     * @throws TransformationFailedException if object (route) is of wrong type
58
     */
59
    public function transform($route)
60
    {
61
        if (null === $route) {
62
            return;
63
        }
64
65
        if (!$route instanceof RouteInterface) {
66
            throw new UnexpectedTypeException($route, RouteInterface::class);
67
        }
68
69
        return $route->getId();
70
    }
71
72
    /**
73
     * Transforms an id to an object (route).
74
     *
75
     * @param string $routeId
76
     *
77
     * @return RouteInterface
78
     *
79
     * @throws TransformationFailedException if object (route) is not found
80
     */
81
    public function reverseTransform($routeId)
82
    {
83
        if (null === $routeId) {
84
            return;
85
        }
86
87
        $tenantCode = $this->tenantContext->getTenant()->getCode();
88
89
        /** @var RouteInterface $route */
90
        $route = $this->routeRepository->findOneBy([
91
            'id' => $routeId,
92
            'tenantCode' => $tenantCode,
93
        ]);
94
95
        if (null === $route) {
96
            throw new TransformationFailedException(sprintf(
97
                'Route with id "%s" does not exist for "%s" tenant!',
98
                $routeId,
99
                $tenantCode
100
            ));
101
        }
102
103
        return $route;
104
    }
105
}
106