Completed
Push — master ( fecb59...e32f72 )
by Paweł
29:36
created

ArticleLoader::getQueryString()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.0291

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 12
cts 13
cp 0.9231
rs 6.6037
c 0
b 0
f 0
cc 8
eloc 14
nc 5
nop 2
crap 8.0291
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 Jackalope\Query\SqlQuery;
20
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\ArticleInterface;
21
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Route;
22
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface;
23
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
24
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
25
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
26
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
27
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
28
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
29
use Doctrine\ODM\PHPCR\DocumentManager;
30
31
/**
32
 * Class ArticleLoader.
33
 */
34
class ArticleLoader implements LoaderInterface
35
{
36
    /**
37
     * @var ArticleProviderInterface
38
     */
39
    protected $articleProvider;
40
41
    /**
42
     * @var RouteProviderInterface
43
     */
44
    protected $routeProvider;
45
46
    /**
47
     * @var DocumentManager
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 DocumentManager          $dm
72
     * @param MetaFactoryInterface     $metaFactory
73
     * @param Context                  $context
74
     */
75 98
    public function __construct(
76
        ArticleProviderInterface $articleProvider,
77
        RouteProviderInterface $routeProvider,
78
        DocumentManager $dm,
79
        MetaFactoryInterface $metaFactory,
80
        Context $context
81
    ) {
82 98
        $this->articleProvider = $articleProvider;
83 98
        $this->routeProvider = $routeProvider;
84 98
        $this->dm = $dm;
85 98
        $this->metaFactory = $metaFactory;
86 98
        $this->context = $context;
87 98
    }
88
89
    /**
90
     * Load meta object by provided type and parameters.
91
     *
92
     * @MetaLoaderDoc(
93
     *     description="Article Loader loads articles from Content Repository",
94
     *     parameters={
95
     *         contentPath="SINGLE|required content path",
96
     *         slug="SINGLE|required content slug",
97
     *         pageName="COLLECTiON|name of Page for required articles"
98
     *     }
99
     * )
100
     *
101
     * @param string $type         object type
102
     * @param array  $parameters   parameters needed to load required object type
103
     * @param int    $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
104
     *
105
     * @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise
106
     *
107
     * @throws \Exception
108
     */
109 18
    public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE)
110
    {
111 18
        $article = null;
112
113 18
        if ($responseType === LoaderInterface::SINGLE) {
114 14
            if (array_key_exists('contentPath', $parameters)) {
115 3
                $article = $this->dm->find('SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article', $parameters['contentPath']);
116 3
                if (null !== $article && !$article->isPublished()) {
117 3
                    $article = null;
118
                }
119 11
            } elseif (array_key_exists('article', $parameters)) {
120
                $this->dm->detach($parameters['article']);
121
                $article = $this->dm->find('SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article', $parameters['article']->getId());
122
                if (null !== $article && !$article->isPublished()) {
123
                    $article = null;
124
                }
125 11
            } elseif (array_key_exists('slug', $parameters)) {
126 11
                $article = $this->dm->getRepository('SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article')->findOneBy([
127 11
                    'slug' => $parameters['slug'],
128 11
                    'status' => ArticleInterface::STATUS_PUBLISHED,
129
                ]);
130
            }
131
132 14
            return $this->getArticleMeta($article);
133 6
        } elseif ($responseType === LoaderInterface::COLLECTION) {
134 6
            $route = null;
135 6
            if (array_key_exists('route', $parameters)) {
136 6
                $route = $this->routeProvider->getOneById($parameters['route']);
137 3
            } elseif (null !== ($currentPage = $this->context->getCurrentPage())) {
138 2
                $route = $currentPage->getValues();
139
            }
140
141 6
            if (null !== $route && is_object($route)) {
142 6
                $query = $this->getRouteArticlesQuery($route, $parameters);
143 5
                $countQuery = clone $query;
144
145 5
                if (isset($parameters['limit'])) {
146 1
                    $query->setLimit($parameters['limit']);
147
                }
148
149 5
                if (isset($parameters['start'])) {
150 1
                    $query->setOffset($parameters['start']);
151
                }
152
153 5
                $articles = $this->dm->getDocumentsByPhpcrQuery($query);
154 5
                $metaCollection = new MetaCollection();
155 5
                $metaCollection->setTotalItemsCount($countQuery->execute()->getRows()->count());
156 5
                foreach ($articles as $article) {
157 5
                    $articleMeta = $this->getArticleMeta($article);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $articleMeta is correct as $this->getArticleMeta($article) (which targets SWP\Bundle\ContentBundle...oader::getArticleMeta()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
158 5
                    if ($articleMeta) {
159 5
                        $metaCollection->add($articleMeta);
160
                    }
161
                }
162
163 5
                return $metaCollection;
164
            }
165
        }
166
167 1
        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 17
    private function getArticleMeta($article)
183
    {
184 17
        if (!is_null($article)) {
185 15
            return $this->metaFactory->create($article);
186
        }
187
188 4
        return;
189
    }
190
191
    /**
192
     * @param Route $route
193
     * @param array $parameters
194
     *
195
     * @return SqlQuery
196
     */
197 6
    private function getRouteArticlesQuery(Route $route, array $parameters) : SqlQuery
198
    {
199 6
        $routeIdentifier = $this->dm->getNodeForDocument($route)->getIdentifier();
200 6
        $order = ['publishedAt', 'DESC'];
201 6
        if (array_key_exists('order', $parameters) && is_array($parameters['order'])) {
202 3
            $order = $parameters['order'] + $order;
203
        }
204
205 6
        return $this->articleProvider->getRouteArticlesQuery($routeIdentifier, $order);
206
    }
207
}
208