Completed
Push — master ( f6198e...300d99 )
by Paweł
53:20
created

ArticleLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
crap 1
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 109
    public function __construct(
77
        ArticleProviderInterface $articleProvider,
78
        RouteProviderInterface $routeProvider,
79
        ObjectManager $dm,
80
        MetaFactoryInterface $metaFactory,
81
        Context $context
82
    ) {
83 109
        $this->articleProvider = $articleProvider;
84 109
        $this->routeProvider = $routeProvider;
85 109
        $this->dm = $dm;
86 109
        $this->metaFactory = $metaFactory;
87 109
        $this->context = $context;
88 109
    }
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 ($type === 'article' && $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 ($type === 'articles' && $responseType === LoaderInterface::COLLECTION) {
127 2
            $currentPage = $this->context['route'];
128 2
            $route = null;
129
130 2
            if ($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 2
                    if (null === $route) {
143
                        // if Route parameter was passed but it was not found - don't return articles not filtered by route
144
                        return;
145
                    }
146
                }
147
            }
148
149 2
            if (null !== $route) {
150 2
                if ($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType()) {
151 2
                    $criteria->set('route', $route);
152
                }
153
            }
154
155 2
            if (isset($parameters['metadata'])) {
156 2
                $criteria->set('metadata', $parameters['metadata']);
157 2
            }
158 2
159 2
            $criteria = $this->applyPaginationToCriteria($criteria, $parameters);
160 2
            $articles = $this->articleProvider->getManyByCriteria($criteria);
161 2
            if ($articles->count() > 0) {
162 2
                $metaCollection = new MetaCollection();
163 2
                $metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($criteria));
164
                foreach ($articles as $article) {
165
                    $articleMeta = $this->getArticleMeta($article);
166 2
                    if (null !== $articleMeta) {
167
                        $metaCollection->add($articleMeta);
168 2
                    }
169
                }
170
                unset($articles, $route, $criteria);
171
172
                return $metaCollection;
173
            }
174
        }
175
176
        return;
177
    }
178
179
    /**
180
     * Checks if Loader supports provided type.
181
     *
182 14
     * @param string $type
183
     *
184 14
     * @return bool
185
     */
186
    public function isSupported(string $type): bool
187 8
    {
188
        return in_array($type, ['articles', 'article']);
189 8
    }
190 8
191
    private function getArticleMeta($article)
192
    {
193
        if (null !== $article) {
194
            return $this->metaFactory->create($article);
195
        }
196
197
        return;
198
    }
199
}
200