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

ArticleToIdTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 64
loc 64
ccs 14
cts 17
cp 0.8235
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\ArticleInterface;
18
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface;
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 ArticleToIdTransformer 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 ArticleProviderInterface
27
     */
28
    private $articleProvider;
29
30
    /**
31
     * ArticleToIdTransformer constructor.
32
     *
33
     * @param ArticleProviderInterface $articleProvider
34
     */
35 16
    public function __construct(ArticleProviderInterface $articleProvider)
36
    {
37 16
        $this->articleProvider = $articleProvider;
38 16
    }
39
40
    /**
41
     * Transforms an object (article) to a string (id).
42
     *
43
     * @param ArticleInterface|string $article
44
     *
45
     * @return string
46
     */
47 16
    public function transform($article)
48
    {
49 16
        if (null === $article) {
50 15
            return;
51
        }
52
53 1
        if (!$article instanceof ArticleInterface) {
54
            throw new UnexpectedTypeException($article, ArticleInterface::class);
55
        }
56
57 1
        return $article->getId();
58
    }
59
60
    /**
61
     * Transforms an id to an object (article).
62
     *
63
     * @param string $articleId
64
     *
65
     * @return ArticleInterface|void
66
     *
67
     * @throws TransformationFailedException if object (article) is not found
68
     */
69 15
    public function reverseTransform($articleId)
70
    {
71 15
        if (null === $articleId) {
72 14
            return;
73
        }
74
75 3
        $article = $this->articleProvider->getOneById($articleId);
76
77 3
        if (null === $article) {
78
            throw new TransformationFailedException(sprintf(
79
                'Article with id "%s" does not exist!',
80
                $article
81
            ));
82
        }
83
84 3
        return $article;
85
    }
86
}
87