Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

ArticleMediaLoader::isSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 SWP\Bundle\ContentBundle\Provider\ArticleMediaProviderInterface;
20
use SWP\Component\Common\Criteria\Criteria;
21
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
22
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactory;
23
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
24
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
25
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
26
27
/**
28
 * Class ArticleMediaLoader.
29
 */
30
class ArticleMediaLoader extends PaginatedLoader implements LoaderInterface
31
{
32
    /**
33
     * @var MetaFactory
34
     */
35
    protected $metaFactory;
36
37
    /**
38
     * @var Context
39
     */
40
    protected $context;
41
42
    /**
43
     * @var ArticleMediaProviderInterface
44
     */
45
    protected $articleMediaProvider;
46
47
    /**
48
     * ArticleMediaLoader constructor.
49
     *
50
     * @param ArticleMediaProviderInterface $articleMediaProvider
51
     * @param MetaFactory                   $metaFactory
52
     * @param Context                       $context
53
     */
54 109
    public function __construct(
55
        ArticleMediaProviderInterface $articleMediaProvider,
56
        MetaFactory $metaFactory,
57
        Context $context
58
    ) {
59 109
        $this->articleMediaProvider = $articleMediaProvider;
60 109
        $this->metaFactory = $metaFactory;
61 109
        $this->context = $context;
62 109
    }
63
64
    /**
65
     * Load meta object by provided type and parameters.
66
     *
67
     * @MetaLoaderDoc(
68
     *     description="Article Media Loader loads article media from Content Repository",
69
     *     parameters={
70
     *         article="COLLECTION| article Meta object"
71
     *     }
72
     * )
73
     *
74
     * @param string $type         object type
75
     * @param array  $parameters   parameters needed to load required object type
76
     * @param int    $responseType response type: collection of meta (LoaderInterface::COLLECTION)
77
     *
78
     * @return Meta[]|bool false if meta cannot be loaded, an array with Meta instances otherwise
79
     */
80 5
    public function load($type, $parameters = [], $responseType = LoaderInterface::COLLECTION)
81
    {
82 5
        if ($responseType === LoaderInterface::COLLECTION) {
83 5
            $criteria = new Criteria();
84 5
            $criteria->set('maxResults', null);
85
86 5
            if (array_key_exists('article', $parameters) && $parameters['article'] instanceof Meta) {
87 2
                $criteria->set('article', $parameters['article']->getValues());
88 5
            } elseif (isset($this->context->article) && null !== $this->context->article) {
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...
89 2
                $criteria->set('article', $this->context->article->getValues());
90
            } else {
91 3
                return false;
92
            }
93
94 2
            $criteria = $this->applyPaginationToCriteria($criteria, $parameters);
95 2
            $media = $this->articleMediaProvider->getManyByCriteria($criteria);
96 2
            if ($media->count() > 0) {
97
                $metaCollection = new MetaCollection();
98
                $metaCollection->setTotalItemsCount($this->articleMediaProvider->getCountByCriteria($criteria));
99
                foreach ($media as $item) {
100
                    $metaCollection->add($this->metaFactory->create($item));
101
                }
102
103
                return $metaCollection;
104
            }
105
        }
106
107 2
        return false;
108
    }
109
110
    /**
111
     * Checks if Loader supports provided type.
112
     *
113
     * @param string $type
114
     *
115
     * @return bool
116
     */
117 11
    public function isSupported(string $type): bool
118
    {
119 11
        return in_array($type, ['articleMedia']);
120
    }
121
}
122