Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

ArticleLoader::getRouteArticlesQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
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 105
    public function __construct(
77
        ArticleProviderInterface $articleProvider,
78
        RouteProviderInterface $routeProvider,
79
        ObjectManager $dm,
80
        MetaFactoryInterface $metaFactory,
81
        Context $context
82
    ) {
83 105
        $this->articleProvider = $articleProvider;
84 105
        $this->routeProvider = $routeProvider;
85 105
        $this->dm = $dm;
86 105
        $this->metaFactory = $metaFactory;
87 105
        $this->context = $context;
88 105
    }
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 18
    public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE)
111
    {
112 18
        $criteria = new Criteria();
113 18
        if ($responseType === LoaderInterface::SINGLE) {
114 14
            if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) {
115
                $this->dm->detach($parameters['article']);
116
                $criteria->set('id', $parameters['article']->getId());
117 14
            } elseif (array_key_exists('slug', $parameters)) {
118 14
                $criteria->set('slug', $parameters['slug']);
119
            }
120
121
            try {
122 14
                return $this->getArticleMeta($this->articleProvider->getOneByCriteria($criteria));
123 4
            } catch (NotFoundHttpException $e) {
124 4
                return;
125
            }
126 6
        } elseif ($responseType === LoaderInterface::COLLECTION) {
127 6
            $currentPage = $this->context->getCurrentPage();
128 6
            $route = null;
129
130 6
            if (null !== $currentPage) {
131 2
                $route = $currentPage->getValues();
132
            }
133
134 6
            if (array_key_exists('route', $parameters)) {
135 6
                if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) {
136 6
                    if (is_int($parameters['route'])) {
137 1
                        $route = $this->routeProvider->getOneById($parameters['route']);
138 6
                    } elseif (is_string($parameters['route'])) {
139 6
                        $route = $this->routeProvider->getOneByStaticPrefix($parameters['route']);
140
                    }
141
                }
142
            }
143
144 6
            if ($route instanceof RouteInterface) {
145 6
                $criteria->set('route', $route);
146
            } else {
147 1
                return;
148
            }
149
150 6
            $criteria = $this->applyPaginationToCriteria($criteria, $parameters);
151 6
            $articles = $this->articleProvider->getManyByCriteria($criteria);
152 5
            if ($articles->count() > 0) {
153 5
                $metaCollection = new MetaCollection();
154 5
                $metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($criteria));
155 5
                foreach ($articles as $article) {
156 5
                    $articleMeta = $this->getArticleMeta($article);
157 5
                    if (null !== $articleMeta) {
158 5
                        $metaCollection->add($articleMeta);
159
                    }
160
                }
161 5
                unset($articles, $route, $criteria);
162
163 5
                return $metaCollection;
164
            }
165
        }
166
167
        return;
168
    }
169
170
    /**
171
     * Checks if Loader supports provided type.
172
     *
173
     * @param string $type
174
     *
175
     * @return bool
176
     */
177 17
    public function isSupported(string $type) : bool
178
    {
179 17
        return in_array($type, ['articles', 'article']);
180
    }
181
182 15
    private function getArticleMeta($article)
183
    {
184 15
        if (null !== $article) {
185 15
            return $this->metaFactory->create($article);
186
        }
187
188
        return;
189
    }
190
}
191