Completed
Push — master ( 4da1f4...8b580e )
by Paweł
08:27
created

ArticleLoader::isSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 109
        ArticleProviderInterface $articleProvider,
77
        RouteProviderInterface $routeProvider,
78
        ObjectManager $dm,
79
        MetaFactoryInterface $metaFactory,
80
        Context $context
81
    ) {
82
        $this->articleProvider = $articleProvider;
83 109
        $this->routeProvider = $routeProvider;
84 109
        $this->dm = $dm;
85 109
        $this->metaFactory = $metaFactory;
86 109
        $this->context = $context;
87 109
    }
88 109
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
                $this->dm->detach($parameters['article']);
99
                $criteria->set('id', $parameters['article']->getId());
100
                unset($parameters['article']);
101
            } elseif (array_key_exists('slug', $parameters)) {
102
                $criteria->set('slug', $parameters['slug']);
103
            }
104
105
            try {
106
                $article = $this->articleProvider->getOneByCriteria($criteria);
107
            } catch (NotFoundHttpException $e) {
108
                return;
109
            }
110 10
111
            try {
112 10
                return $this->getArticleMeta($article);
113 10
            } catch (NotFoundHttpException $e) {
114 9
                return;
115
            }
116
        } elseif ('articles' === $type && LoaderInterface::COLLECTION === $responseType) {
117 9
            $currentPage = $this->context['route'];
118 9
            $route = null;
119
120
            if ($currentPage) {
121
                $route = $currentPage->getValues();
122 9
            }
123 2
124 2
            if (array_key_exists('route', $parameters)) {
125
                if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) {
126 2
                    if (is_int($parameters['route'])) {
127 2
                        $route = $this->routeProvider->getOneById($parameters['route']);
128 2
                    } elseif (is_string($parameters['route'])) {
129
                        $route = $this->routeProvider->getOneByStaticPrefix($parameters['route']);
130 2
                    } elseif (is_array($parameters['route'])) {
131 2
                        $loadByStaticPrefix = true;
132
                        foreach ($parameters['route'] as $key => $providedRoute) {
133
                            if (!is_string($providedRoute)) {
134 2
                                $loadByStaticPrefix = false;
135 2
136 2
                                break;
137
                            }
138 2
                        }
139 2
140
                        if ($loadByStaticPrefix) {
141
                            $route = $this->routeProvider->getWithChildrenByStaticPrefix($parameters['route']);
142 2
                        } else {
143
                            $route = $parameters['route'];
144
                        }
145
                    }
146
147
                    if (null === $route) {
148
                        // if Route parameter was passed but it was not found - don't return articles not filtered by route
149 2
                        return;
150 2
                    }
151 2
                }
152
            }
153
154
            if (null !== $route && ($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType() || is_array($route))) {
155 2
                $criteria->set('route', $route);
156 2
            }
157 2
158 2
            foreach (['metadata', 'keywords', 'source', 'author'] as $item) {
159 2
                if (isset($parameters[$item])) {
160 2
                    $criteria->set($item, $parameters[$item]);
161 2
                }
162 2
163 2
                if (isset($withoutParameters[$item])) {
164
                    $criteria->set('exclude_'.$item, $withoutParameters[$item]);
165
                }
166 2
            }
167
168 2
            $criteria = $this->applyPaginationToCriteria($criteria, $parameters);
169
            $countCriteria = clone $criteria;
170
            $articlesCollection = $this->articleProvider->getManyByCriteria($criteria, $criteria->get('order', []));
171
172
            if ($articlesCollection->count() > 0) {
173
                $metaCollection = new MetaCollection();
174
                $metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($countCriteria));
175
                foreach ($articlesCollection as $article) {
176
                    $articleMeta = $this->getArticleMeta($article);
177
                    if (null !== $articleMeta) {
178
                        $metaCollection->add($articleMeta);
179
                    }
180
                }
181
                unset($articlesCollection, $route, $criteria);
182 14
183
                return $metaCollection;
184 14
            }
185
        }
186
187 8
        return;
188
    }
189 8
190 8
    /**
191
     * Checks if Loader supports provided type.
192
     *
193
     * @param string $type
194
     *
195
     * @return bool
196
     */
197
    public function isSupported(string $type): bool
198
    {
199
        return in_array($type, ['articles', 'article']);
200
    }
201
202
    /**
203
     * @param ArticleInterface|null $article
204
     *
205
     * @return \SWP\Component\TemplatesSystem\Gimme\Meta\Meta|void
206
     */
207
    private function getArticleMeta($article)
208
    {
209
        if (null !== $article) {
210
            return $this->metaFactory->create($article);
211
        }
212
213
        return;
214
    }
215
}
216