1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2015 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Loader; |
18
|
|
|
|
19
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
20
|
|
|
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface; |
21
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
25
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
26
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface; |
27
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
28
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection; |
29
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class ArticleLoader. |
33
|
|
|
*/ |
34
|
|
|
class ArticleLoader extends PaginatedLoader implements LoaderInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ArticleProviderInterface |
38
|
|
|
*/ |
39
|
|
|
protected $articleProvider; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var RouteProviderInterface |
43
|
|
|
*/ |
44
|
|
|
protected $routeProvider; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ObjectManager |
48
|
|
|
*/ |
49
|
|
|
protected $dm; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $routeBasepaths; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var MetaFactoryInterface |
58
|
|
|
*/ |
59
|
|
|
protected $metaFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Context |
63
|
|
|
*/ |
64
|
|
|
protected $context; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* ArticleLoader constructor. |
68
|
|
|
* |
69
|
|
|
* @param ArticleProviderInterface $articleProvider |
70
|
|
|
* @param RouteProviderInterface $routeProvider |
71
|
|
|
* @param ObjectManager $dm |
72
|
|
|
* @param MetaFactoryInterface $metaFactory |
73
|
|
|
* @param Context $context |
74
|
|
|
*/ |
75
|
|
|
public function __construct( |
76
|
111 |
|
ArticleProviderInterface $articleProvider, |
77
|
|
|
RouteProviderInterface $routeProvider, |
78
|
|
|
ObjectManager $dm, |
79
|
|
|
MetaFactoryInterface $metaFactory, |
80
|
|
|
Context $context |
81
|
|
|
) { |
82
|
|
|
$this->articleProvider = $articleProvider; |
83
|
111 |
|
$this->routeProvider = $routeProvider; |
84
|
111 |
|
$this->dm = $dm; |
85
|
111 |
|
$this->metaFactory = $metaFactory; |
86
|
111 |
|
$this->context = $context; |
87
|
111 |
|
} |
88
|
111 |
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE) |
93
|
|
|
{ |
94
|
|
|
$criteria = new Criteria(); |
95
|
|
|
if ('article' === $type && LoaderInterface::SINGLE === $responseType) { |
96
|
|
|
$article = null; |
97
|
|
|
if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { |
98
|
|
|
try { |
99
|
|
|
return $this->getArticleMeta($parameters['article']); |
100
|
|
|
} catch (NotFoundHttpException $e) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
} elseif (array_key_exists('slug', $parameters)) { |
104
|
|
|
$criteria->set('slug', $parameters['slug']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
$article = $this->articleProvider->getOneByCriteria($criteria); |
109
|
|
|
|
110
|
10 |
|
return $this->getArticleMeta($article); |
111
|
|
|
} catch (NotFoundHttpException $e) { |
112
|
10 |
|
return; |
113
|
10 |
|
} |
114
|
9 |
|
} elseif ('articles' === $type && LoaderInterface::COLLECTION === $responseType) { |
115
|
|
|
$currentPage = $this->context['route']; |
116
|
|
|
$route = null; |
117
|
9 |
|
|
118
|
9 |
|
if ($currentPage) { |
119
|
|
|
$route = $currentPage->getValues(); |
120
|
|
|
} |
121
|
|
|
|
122
|
9 |
|
if (array_key_exists('route', $parameters)) { |
123
|
2 |
|
if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) { |
124
|
2 |
|
$route = $this->routeProvider->getByMixed($parameters['route']); |
125
|
|
|
|
126
|
2 |
|
if (null === $route) { |
127
|
2 |
|
// if Route parameter was passed but it was not found - don't return articles not filtered by route |
128
|
2 |
|
return; |
129
|
|
|
} |
130
|
2 |
|
} |
131
|
2 |
|
} |
132
|
|
|
|
133
|
|
|
if (null !== $route && ($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType() || is_array($route))) { |
134
|
2 |
|
$criteria->set('route', $route); |
135
|
2 |
|
} |
136
|
2 |
|
|
137
|
|
|
foreach (['metadata', 'extra', 'keywords', 'source', 'author', 'article', 'publishedAfter', 'publishedBefore'] as $item) { |
138
|
2 |
|
if (isset($parameters[$item])) { |
139
|
2 |
|
$criteria->set($item, $parameters[$item]); |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
if (isset($withoutParameters[$item])) { |
143
|
|
|
$criteria->set('exclude_'.$item, $withoutParameters[$item]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->applyPaginationToCriteria($criteria, $parameters); |
148
|
|
|
$this->setDateRangeToCriteria($criteria, $parameters); |
149
|
2 |
|
$countCriteria = clone $criteria; |
150
|
2 |
|
$articlesCollection = $this->articleProvider->getManyByCriteria($criteria, $criteria->get('order', [])); |
151
|
2 |
|
|
152
|
|
View Code Duplication |
if ($articlesCollection->count() > 0) { |
|
|
|
|
153
|
|
|
$metaCollection = new MetaCollection(); |
154
|
|
|
$metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($countCriteria)); |
155
|
2 |
|
foreach ($articlesCollection as $article) { |
156
|
|
|
$articleMeta = $this->getArticleMeta($article); |
157
|
|
|
if (null !== $articleMeta) { |
158
|
|
|
$metaCollection->add($articleMeta); |
159
|
2 |
|
} |
160
|
2 |
|
} |
161
|
2 |
|
unset($articlesCollection, $route, $criteria); |
162
|
2 |
|
|
163
|
2 |
|
return $metaCollection; |
164
|
2 |
|
} |
165
|
2 |
|
} |
166
|
2 |
|
} |
167
|
2 |
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
2 |
|
*/ |
171
|
|
|
public function isSupported(string $type): bool |
172
|
2 |
|
{ |
173
|
|
|
return in_array($type, ['articles', 'article']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param Criteria $criteria |
178
|
|
|
* @param array $parameters |
179
|
|
|
*/ |
180
|
|
|
protected function setDateRangeToCriteria(Criteria $criteria, array $parameters) |
181
|
|
|
{ |
182
|
|
|
if (isset($parameters['date_range']) && is_array($parameters['date_range']) && 2 === count($parameters['date_range'])) { |
183
|
|
|
$criteria->set('dateRange', $parameters['date_range']); |
184
|
|
|
} |
185
|
|
|
} |
186
|
14 |
|
|
187
|
|
|
/** |
188
|
14 |
|
* @param ArticleInterface|null $article |
189
|
|
|
* |
190
|
|
|
* @return \SWP\Component\TemplatesSystem\Gimme\Meta\Meta|void |
191
|
8 |
|
*/ |
192
|
|
|
protected function getArticleMeta($article) |
193
|
8 |
|
{ |
194
|
8 |
|
if (null !== $article) { |
195
|
|
|
return $this->metaFactory->create($article); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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.