Completed
Push — master ( 05bb5a...5289b8 )
by Rafał
34:32 queued 04:15
created

RelatedArticleLoader::load()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.1475
c 0
b 0
f 0
cc 8
nc 10
nop 4
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 2019 Sourcefabric z.ú. 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 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Loader;
18
19
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
20
use SWP\Bundle\ContentBundle\Doctrine\RelatedArticleRepositoryInterface;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
23
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
24
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
25
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
26
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
27
28
final class RelatedArticleLoader extends PaginatedLoader implements LoaderInterface
29
{
30
    public const SUPPORTED_TYPE = 'relatedArticles';
31
32
    /**
33
     * @var ArticleRepositoryInterface
34
     */
35
    protected $articleRepository;
36
37
    /**
38
     * @var RelatedArticleRepositoryInterface
39
     */
40
    protected $relatedArticleRepository;
41
42
    /**
43
     * @var MetaFactoryInterface
44
     */
45
    protected $metaFactory;
46
47
    /**
48
     * @var Context
49
     */
50
    protected $context;
51
52
    public function __construct(
53
        ArticleRepositoryInterface $articleRepository,
54
        RelatedArticleRepositoryInterface $relatedArticleRepository,
55
        MetaFactoryInterface $metaFactory,
56
        Context $context
57
    ) {
58
        $this->articleRepository = $articleRepository;
59
        $this->relatedArticleRepository = $relatedArticleRepository;
60
        $this->metaFactory = $metaFactory;
61
        $this->context = $context;
62
    }
63
64
    public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
65
    {
66
        if (LoaderInterface::COLLECTION === $responseType) {
67
            $criteria = new Criteria();
68
            if (array_key_exists('article', $parameters) && $parameters['article'] instanceof Meta) {
69
                $criteria->set('article', $parameters['article']->getValues());
70
            } elseif (isset($this->context->article)) {
71
                $criteria->set('article', $this->context->article->getValues());
0 ignored issues
show
Bug introduced by
The property article does not seem to exist in SWP\Component\TemplatesS...m\Gimme\Context\Context.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
72
            } else {
73
                return false;
74
            }
75
76
            $this->applyPaginationToCriteria($criteria, $parameters);
77
            $relatedArticles = $this->relatedArticleRepository->getByCriteria($criteria, $criteria->get('order', []));
78
            $relatedArticlesCount = $this->relatedArticleRepository->countByCriteria($criteria);
79
80
            if (\count($relatedArticles) > 0) {
81
                $metaCollection = new MetaCollection();
82
                $metaCollection->setTotalItemsCount($relatedArticlesCount);
83
84
                foreach ($relatedArticles as $relatedArticle) {
85
                    $meta = $this->metaFactory->create($relatedArticle);
86
                    if (null !== $meta) {
87
                        $metaCollection->add($meta);
88
                    }
89
                }
90
91
                unset($articlesCollection, $route, $criteria);
92
93
                return $metaCollection;
94
            }
95
        }
96
    }
97
98
    public function isSupported(string $type): bool
99
    {
100
        return self::SUPPORTED_TYPE === $type && !$this->context->isPreviewMode();
101
    }
102
}
103