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\Component\Common\Criteria\Criteria; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface; |
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\Meta; |
29
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class ArticleLoader. |
34
|
|
|
*/ |
35
|
|
|
class ArticleLoader extends PaginatedLoader implements LoaderInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var ArticleProviderInterface |
39
|
|
|
*/ |
40
|
|
|
protected $articleProvider; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var RouteProviderInterface |
44
|
|
|
*/ |
45
|
|
|
protected $routeProvider; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ObjectManager |
49
|
|
|
*/ |
50
|
|
|
protected $dm; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $routeBasepaths; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var MetaFactoryInterface |
59
|
|
|
*/ |
60
|
|
|
protected $metaFactory; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Context |
64
|
|
|
*/ |
65
|
|
|
protected $context; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* ArticleLoader constructor. |
69
|
|
|
* |
70
|
|
|
* @param ArticleProviderInterface $articleProvider |
71
|
|
|
* @param RouteProviderInterface $routeProvider |
72
|
|
|
* @param ObjectManager $dm |
73
|
|
|
* @param MetaFactoryInterface $metaFactory |
74
|
|
|
* @param Context $context |
75
|
|
|
*/ |
76
|
91 |
|
public function __construct( |
77
|
|
|
ArticleProviderInterface $articleProvider, |
78
|
|
|
RouteProviderInterface $routeProvider, |
79
|
|
|
ObjectManager $dm, |
80
|
|
|
MetaFactoryInterface $metaFactory, |
81
|
|
|
Context $context |
82
|
|
|
) { |
83
|
91 |
|
$this->articleProvider = $articleProvider; |
84
|
91 |
|
$this->routeProvider = $routeProvider; |
85
|
91 |
|
$this->dm = $dm; |
86
|
91 |
|
$this->metaFactory = $metaFactory; |
87
|
91 |
|
$this->context = $context; |
88
|
91 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Load meta object by provided type and parameters. |
92
|
|
|
* |
93
|
|
|
* @MetaLoaderDoc( |
94
|
|
|
* description="Article Loader loads articles from Content Repository", |
95
|
|
|
* parameters={ |
96
|
|
|
* contentPath="SINGLE|required content path", |
97
|
|
|
* slug="SINGLE|required content slug", |
98
|
|
|
* pageName="COLLECTiON|name of Page for required articles" |
99
|
|
|
* } |
100
|
|
|
* ) |
101
|
|
|
* |
102
|
|
|
* @param string $type object type |
103
|
|
|
* @param array $parameters parameters needed to load required object type |
104
|
|
|
* @param int $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION) |
105
|
|
|
* |
106
|
|
|
* @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise |
107
|
|
|
* |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
10 |
|
public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE) |
111
|
|
|
{ |
112
|
10 |
|
$criteria = new Criteria(); |
113
|
10 |
|
if ($responseType === LoaderInterface::SINGLE) { |
114
|
9 |
|
if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { |
115
|
|
|
$this->dm->detach($parameters['article']); |
116
|
|
|
$criteria->set('id', $parameters['article']->getId()); |
117
|
9 |
|
} elseif (array_key_exists('slug', $parameters)) { |
118
|
9 |
|
$criteria->set('slug', $parameters['slug']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
try { |
122
|
9 |
|
return $this->getArticleMeta($this->articleProvider->getOneByCriteria($criteria)); |
123
|
2 |
|
} catch (NotFoundHttpException $e) { |
124
|
2 |
|
return; |
125
|
|
|
} |
126
|
2 |
|
} elseif ($responseType === LoaderInterface::COLLECTION) { |
127
|
2 |
|
$currentPage = $this->context->getCurrentPage(); |
128
|
2 |
|
$route = null; |
129
|
|
|
|
130
|
2 |
|
if (null !== $currentPage) { |
131
|
2 |
|
$route = $currentPage->getValues(); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
if (array_key_exists('route', $parameters)) { |
135
|
2 |
|
if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) { |
136
|
2 |
|
if (is_int($parameters['route'])) { |
137
|
|
|
$route = $this->routeProvider->getOneById($parameters['route']); |
138
|
2 |
|
} elseif (is_string($parameters['route'])) { |
139
|
2 |
|
$route = $this->routeProvider->getOneByStaticPrefix($parameters['route']); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
if (null === $route) { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
if ($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType()) { |
149
|
2 |
|
$criteria->set('route', $route); |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
$criteria = $this->applyPaginationToCriteria($criteria, $parameters); |
153
|
2 |
|
$articles = $this->articleProvider->getManyByCriteria($criteria); |
154
|
2 |
|
if ($articles->count() > 0) { |
155
|
2 |
|
$metaCollection = new MetaCollection(); |
156
|
2 |
|
$metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($criteria)); |
157
|
2 |
|
foreach ($articles as $article) { |
158
|
2 |
|
$articleMeta = $this->getArticleMeta($article); |
159
|
2 |
|
if (null !== $articleMeta) { |
160
|
2 |
|
$metaCollection->add($articleMeta); |
161
|
|
|
} |
162
|
|
|
} |
163
|
2 |
|
unset($articles, $route, $criteria); |
164
|
|
|
|
165
|
2 |
|
return $metaCollection; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Checks if Loader supports provided type. |
174
|
|
|
* |
175
|
|
|
* @param string $type |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
14 |
|
public function isSupported(string $type): bool |
180
|
|
|
{ |
181
|
14 |
|
return in_array($type, ['articles', 'article']); |
182
|
|
|
} |
183
|
|
|
|
184
|
8 |
|
private function getArticleMeta($article) |
185
|
|
|
{ |
186
|
8 |
|
if (null !== $article) { |
187
|
8 |
|
return $this->metaFactory->create($article); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|