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

ArticleToIdTransformer::transform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3.0416
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