|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ExtRefTransformer class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\DocumentBundle\Form\DataTransformer; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\DocumentBundle\Entity\ExtReference; |
|
9
|
|
|
use Graviton\DocumentBundle\Service\ExtReferenceConverterInterface; |
|
10
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
11
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class ExtRefTransformer implements DataTransformerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ExtReferenceConverterInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $converter; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor |
|
27
|
|
|
* |
|
28
|
|
|
* @param ExtReferenceConverterInterface $converter Ext reference converter |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ExtReferenceConverterInterface $converter) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->converter = $converter; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Transforms an object (extref) to a string (url) |
|
37
|
|
|
* |
|
38
|
|
|
* @param ExtReference|null $extref extref object |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function transform($extref) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
if ($extref === null) { |
|
44
|
|
|
return ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
return $this->converter->getUrl($extref); |
|
49
|
|
|
} catch (\InvalidArgumentException $e) { |
|
50
|
|
|
throw new TransformationFailedException( |
|
51
|
|
|
sprintf('Cannot transform extref "%s" to URL', json_encode($extref)), |
|
52
|
|
|
0, |
|
53
|
|
|
$e |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Transforms a string (url) to an object (extref) |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $url extref url |
|
62
|
|
|
* @return ExtReference|null |
|
63
|
|
|
* @throws TransformationFailedException if url is not valid |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function reverseTransform($url) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
if ($url === '' || $url === null) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
return $this->converter->getExtReference($url); |
|
73
|
|
|
} catch (\InvalidArgumentException $e) { |
|
74
|
|
|
throw new TransformationFailedException( |
|
75
|
|
|
sprintf('Cannot transform URL "%s" to extref', $url), |
|
76
|
|
|
0, |
|
77
|
|
|
$e |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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.