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