Completed
Push — master ( 586166...fecb59 )
by Paweł
47:58
created

ArticleMediaLoader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 98
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D load() 0 35 10
A isSupported() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Loader;
16
17
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
18
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
19
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactory;
20
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
21
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
22
use Doctrine\ODM\PHPCR\DocumentManager;
23
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
24
25
/**
26
 * Class ArticleMediaLoader.
27
 */
28
class ArticleMediaLoader implements LoaderInterface
29
{
30
    /**
31
     * @var DocumentManager
32
     */
33
    protected $dm;
34
35
    /**
36
     * @var MetaFactory
37
     */
38
    protected $metaFactory;
39
40
    /**
41
     * @var Context
42
     */
43
    protected $context;
44
45
    /**
46
     * ArticleMediaLoader constructor.
47
     *
48
     * @param DocumentManager $dm
49
     * @param MetaFactory     $metaFactory
50
     * @param Context         $context
51
     */
52 95
    public function __construct(
53
        DocumentManager $dm,
54
        MetaFactory $metaFactory,
55
        Context $context
56
    ) {
57 95
        $this->dm = $dm;
58 95
        $this->metaFactory = $metaFactory;
59 95
        $this->context = $context;
60 95
    }
61
62
    /**
63
     * Load meta object by provided type and parameters.
64
     *
65
     * @MetaLoaderDoc(
66
     *     description="Article Media Loader loads article media from Content Repository",
67
     *     parameters={
68
     *         article="COLLECTION| article Meta object"
69
     *     }
70
     * )
71
     *
72
     * @param string $type         object type
73
     * @param array  $parameters   parameters needed to load required object type
74
     * @param int    $responseType response type: collection of meta (LoaderInterface::COLLECTION)
75
     *
76
     * @return Meta[]|bool false if meta cannot be loaded, an array with Meta instances otherwise
77
     */
78 9
    public function load($type, $parameters = [], $responseType = LoaderInterface::COLLECTION)
79
    {
80 9
        if ($responseType === LoaderInterface::COLLECTION) {
81 9
            $media = false;
82 9
            if (array_key_exists('article', $parameters) && $parameters['article'] instanceof Meta) {
83 7
                $media = $this->dm->find(null, $parameters['article']->getValues()->getId().'/'.ArticleMediaInterface::PATH_MEDIA);
84 9
            } 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...
85 7
                $media = $this->dm->find(null, $this->context->article->getValues()->getId().'/'.ArticleMediaInterface::PATH_MEDIA);
86
            }
87
88 9
            if ($media) {
89 4
                $items = $media->getChildren();
90 4
                $metaCollection = new MetaCollection();
91 4
                $metaCollection->setTotalItemsCount($items->count());
92
93 4
                if (isset($parameters['limit'])) {
94
                    if (isset($parameters['start'])) {
95
                        $start = $parameters['start'];
96
                    } else {
97
                        $start = 0;
98
                    }
99
100
                    $items = $items->slice($start, $parameters['limit']);
101
                }
102
103 4
                foreach ($items as $item) {
104 2
                    $metaCollection->add($this->metaFactory->create($item));
105
                }
106
107 4
                return $metaCollection;
108
            }
109
        }
110
111 5
        return false;
112
    }
113
114
    /**
115
     * Checks if Loader supports provided type.
116
     *
117
     * @param string $type
118
     *
119
     * @return bool
120
     */
121 14
    public function isSupported($type)
122
    {
123 14
        return in_array($type, ['articleMedia']);
124
    }
125
}
126