Completed
Push — master ( 586166...fecb59 )
by Paweł
47:58
created

RouteToIdTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 66
loc 66
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A transform() 12 12 3
A reverseTransform() 17 17 3

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
/**
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
0 ignored issues
show
Duplication introduced by
This class 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...
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