Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

PreviewArticleMediaLoader::load()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 36

Duplication

Lines 3
Ratio 8.33 %

Importance

Changes 0
Metric Value
dl 3
loc 36
rs 8.0995
c 0
b 0
f 0
cc 8
nc 8
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. 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 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Loader;
16
17
use SWP\Bundle\ContentBundle\Loader\PaginatedLoader;
18
use SWP\Component\Common\Criteria\Criteria;
19
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
20
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactory;
21
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
22
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
23
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
24
25
class PreviewArticleMediaLoader extends PaginatedLoader implements LoaderInterface
26
{
27
    /**
28
     * @var MetaFactory
29
     */
30
    protected $metaFactory;
31
32
    /**
33
     * @var Context
34
     */
35
    protected $context;
36
37
    /**
38
     * ArticleMediaLoader constructor.
39
     *
40
     * @param MetaFactory $metaFactory
41
     * @param Context     $context
42
     */
43
    public function __construct(MetaFactory $metaFactory, Context $context)
44
    {
45
        $this->metaFactory = $metaFactory;
46
        $this->context = $context;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function load($metaType, $withParameters = [], $withoutParameters = [], $responseType = self::SINGLE)
53
    {
54
        if (LoaderInterface::COLLECTION === $responseType) {
55
            $criteria = new Criteria();
56
            if (array_key_exists('article', $withParameters) && $withParameters['article'] instanceof Meta) {
57
                $article = $withParameters['article']->getValues();
58 View Code Duplication
            } 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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $article = $this->context->article->getValues();
60
            } else {
61
                return false;
62
            }
63
64
            $criteria = $this->applyPaginationToCriteria($criteria, $withParameters);
65
            $articleMedia = $article->getMedia();
66
            if (0 < count($articleMedia)) {
67
                $collectionCriteria = new \Doctrine\Common\Collections\Criteria(
68
                    null,
69
                    $criteria->get('order'),
70
                    $criteria->get('firstResult'),
71
                    $criteria->get('maxResults')
72
                );
73
                $count = $articleMedia->count();
74
                $articleMedia = $articleMedia->matching($collectionCriteria);
75
76
                $metaCollection = new MetaCollection();
77
                $metaCollection->setTotalItemsCount($count);
78
                foreach ($articleMedia as $media) {
79
                    $metaCollection->add($this->metaFactory->create($media));
80
                }
81
82
                return $metaCollection;
83
            }
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function isSupported(string $type): bool
93
    {
94
        return in_array($type, ['articleMedia']) && $this->context->isPreviewMode();
95
    }
96
}
97