Completed
Push — 1.4 ( f8c621...762927 )
by Rafał
22:37 queued 11:18
created

PreviewSlideshowLoader::load()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 7.1587
c 0
b 0
f 0
cc 10
nc 13
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 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 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Loader;
18
19
use Doctrine\Common\Collections\Criteria as CollectionCriteria;
20
use SWP\Bundle\ContentBundle\Loader\PaginatedLoader;
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 PreviewSlideshowLoader extends PaginatedLoader implements LoaderInterface
29
{
30
    public const SUPPORTED_TYPES = ['slideshows', 'slideshow'];
31
32
    /**
33
     * @var MetaFactoryInterface
34
     */
35
    private $metaFactory;
36
37
    /**
38
     * @var Context
39
     */
40
    protected $context;
41
42
    public function __construct(
43
        MetaFactoryInterface $metaFactory,
44
        Context $context
45
    ) {
46
        $this->metaFactory = $metaFactory;
47
        $this->context = $context;
48
    }
49
50
    public function load($type, $withParameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
51
    {
52
        $criteria = new Criteria();
53
54
        if (array_key_exists('article', $withParameters) && $withParameters['article'] instanceof Meta) {
55
            $article = $withParameters['article']->getValues();
56
        } elseif (isset($this->context->article)) {
57
            $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...
58
        } else {
59
            return false;
60
        }
61
62
        if (LoaderInterface::SINGLE === $responseType) {
63
            if (!(array_key_exists('name', $withParameters) && \is_string($withParameters['name']))) {
64
                return false;
65
            }
66
67
            $criteria = CollectionCriteria::create()->where(CollectionCriteria::expr()->eq('code', $withParameters['name']));
68
            $slideshow = $article->getSlideshows()->matching($criteria)->first();
69
70
            if (null !== $slideshow) {
71
                return $this->metaFactory->create($slideshow);
72
            }
73
74
            return false;
75
        }
76
77
        $criteria = $this->applyPaginationToCriteria($criteria, $withParameters);
78
        $articleMedia = $article->getSlideshows();
79
80
        if (0 < \count($articleMedia)) {
81
            $collectionCriteria = new CollectionCriteria(
82
                null,
83
                $criteria->get('order'),
84
                $criteria->get('firstResult'),
85
                $criteria->get('maxResults')
86
            );
87
88
            $count = $articleMedia->count();
89
90
            $articleMedia = $articleMedia->matching($collectionCriteria);
91
92
            $metaCollection = new MetaCollection();
93
            $metaCollection->setTotalItemsCount($count);
94
            foreach ($articleMedia as $media) {
95
                $metaCollection->add($this->metaFactory->create($media));
96
            }
97
98
            return $metaCollection;
99
        }
100
101
        return false;
102
    }
103
104
    public function isSupported(string $type): bool
105
    {
106
        return \in_array($type, self::SUPPORTED_TYPES, true) && $this->context->isPreviewMode();
107
    }
108
}
109