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

ExtRefTransformer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%
Metric Value
wmc 8
lcom 1
cbo 2
dl 32
loc 64
ccs 0
cts 34
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 16 16 3
A reverseTransform() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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