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\Factory; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\ArticleInterface; |
18
|
|
|
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface; |
19
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
20
|
|
|
use SWP\Component\Bridge\Model\ItemInterface; |
21
|
|
|
use SWP\Component\Bridge\Model\PackageInterface; |
22
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
23
|
|
|
|
24
|
|
|
class ArticleFactory implements ArticleFactoryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var FactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $baseFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RouteProviderInterface |
33
|
|
|
*/ |
34
|
|
|
private $routeProvider; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ArticleProviderInterface |
38
|
|
|
*/ |
39
|
|
|
private $articleProvider; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $contentRelativePath; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $allowedTypes = [ |
50
|
|
|
ItemInterface::TYPE_PICTURE, |
51
|
|
|
ItemInterface::TYPE_FILE, |
52
|
|
|
ItemInterface::TYPE_TEXT, |
53
|
|
|
ItemInterface::TYPE_COMPOSITE, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* ArticleFactory constructor. |
58
|
|
|
* |
59
|
|
|
* @param FactoryInterface $baseFactory |
60
|
|
|
* @param RouteProviderInterface $routeProvider |
61
|
|
|
* @param ArticleProviderInterface $articleProvider |
62
|
|
|
* @param string $contentRelativePath |
63
|
|
|
*/ |
64
|
13 |
|
public function __construct( |
65
|
|
|
FactoryInterface $baseFactory, |
66
|
|
|
RouteProviderInterface $routeProvider, |
67
|
|
|
ArticleProviderInterface $articleProvider, |
68
|
|
|
$contentRelativePath |
69
|
|
|
) { |
70
|
13 |
|
$this->baseFactory = $baseFactory; |
71
|
13 |
|
$this->routeProvider = $routeProvider; |
72
|
13 |
|
$this->articleProvider = $articleProvider; |
73
|
13 |
|
$this->contentRelativePath = $contentRelativePath; |
74
|
13 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
13 |
|
public function create() |
80
|
|
|
{ |
81
|
13 |
|
return $this->baseFactory->create(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
13 |
|
public function createFromPackage(PackageInterface $package) |
88
|
|
|
{ |
89
|
|
|
/** @var ArticleInterface $article */ |
90
|
13 |
|
$article = $this->create(); |
91
|
|
|
|
92
|
13 |
|
$article->setBody($this->populateBody($package)); |
93
|
13 |
|
$article->setParentDocument($this->articleProvider->getParent($this->contentRelativePath)); |
94
|
|
|
|
95
|
13 |
|
$article->setTitle($package->getHeadline()); |
96
|
13 |
|
if (null !== $package->getSlugline()) { |
97
|
13 |
|
$article->setSlug($package->getSlugline()); |
98
|
|
|
} |
99
|
|
|
|
100
|
13 |
|
$article->setLocale($package->getLanguage()); |
101
|
13 |
|
$article->setLead($this->populateLead($package)); |
102
|
|
|
// assign default route |
103
|
13 |
|
$article->setRoute($this->routeProvider->getRouteForArticle($article)); |
104
|
13 |
|
$article->setMetadata($package->getMetadata()); |
105
|
|
|
|
106
|
13 |
|
return $article; |
107
|
|
|
} |
108
|
|
|
|
109
|
13 |
View Code Duplication |
private function populateLead(PackageInterface $package) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
return trim($package->getDescription().implode('', array_map(function (ItemInterface $item) { |
112
|
11 |
|
$this->ensureTypeIsAllowed($item->getType()); |
113
|
|
|
|
114
|
11 |
|
return ' '.$item->getDescription(); |
115
|
13 |
|
}, $package->getItems()->toArray()))); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
private function populateBody(PackageInterface $package) |
|
|
|
|
119
|
|
|
{ |
120
|
13 |
|
return $package->getBody().' '.implode('', array_map(function (ItemInterface $item) { |
121
|
11 |
|
$this->ensureTypeIsAllowed($item->getType()); |
122
|
|
|
|
123
|
11 |
|
return $item->getBody(); |
124
|
13 |
|
}, $package->getItems()->toArray())); |
125
|
|
|
} |
126
|
|
|
|
127
|
11 |
|
private function ensureTypeIsAllowed($type) |
128
|
|
|
{ |
129
|
11 |
|
if (!in_array($type, $this->allowedTypes)) { |
130
|
|
|
throw new \InvalidArgumentException(sprintf( |
131
|
|
|
'Item type "%s" is not supported. Supported types are: %s', |
132
|
|
|
$type, |
133
|
|
|
implode(', ', $this->allowedTypes) |
134
|
|
|
)); |
135
|
|
|
} |
136
|
11 |
|
} |
137
|
|
|
} |
138
|
|
|
|
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.