1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Form\DataTransformer; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
18
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
19
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
20
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
21
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
22
|
|
|
|
23
|
|
View Code Duplication |
final class RouteToIdTransformer implements DataTransformerInterface |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var RouteProviderInterface |
27
|
|
|
*/ |
28
|
|
|
private $routeProvider; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* RouteToIdTransformer constructor. |
32
|
|
|
* |
33
|
|
|
* @param RouteProviderInterface $routeProvider |
34
|
|
|
*/ |
35
|
9 |
|
public function __construct(RouteProviderInterface $routeProvider) |
36
|
|
|
{ |
37
|
9 |
|
$this->routeProvider = $routeProvider; |
38
|
9 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Transforms an object (route) to a string (id). |
42
|
|
|
* |
43
|
|
|
* @param RouteInterface|string $route |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
* |
47
|
|
|
* @throws TransformationFailedException if object (route) is of wrong type |
48
|
|
|
*/ |
49
|
9 |
|
public function transform($route) |
50
|
|
|
{ |
51
|
9 |
|
if (null === $route) { |
52
|
2 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
9 |
|
if (!$route instanceof RouteInterface) { |
56
|
|
|
throw new UnexpectedTypeException($route, RouteInterface::class); |
57
|
|
|
} |
58
|
|
|
|
59
|
9 |
|
return $route->getId(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Transforms an id to an object (route). |
64
|
|
|
* |
65
|
|
|
* @param string $routeId |
66
|
|
|
* |
67
|
|
|
* @return RouteInterface |
68
|
|
|
* |
69
|
|
|
* @throws TransformationFailedException if object (route) is not found |
70
|
|
|
*/ |
71
|
4 |
|
public function reverseTransform($routeId) |
72
|
|
|
{ |
73
|
4 |
|
if (null === $routeId) { |
74
|
2 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
$route = $this->routeProvider->getOneById($routeId); |
78
|
|
|
|
79
|
4 |
|
if (null === $route) { |
80
|
1 |
|
throw new TransformationFailedException(sprintf( |
81
|
1 |
|
'Route with id "%s" does not exist!', |
82
|
|
|
$routeId |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return $route; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.