Completed
Push — feature/fix-somescrutinizer-is... ( 03fa6a...7ccc0d )
by Narcotic
09:35
created

ExtRefTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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