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

PreviewSlideshowItemLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\ArrayCollection;
20
use SWP\Bundle\ContentBundle\Loader\PaginatedLoader;
21
use SWP\Bundle\ContentBundle\Model\SlideshowInterface;
22
use SWP\Component\Common\Criteria\Criteria;
23
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
24
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
25
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
26
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
27
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
28
29
final class PreviewSlideshowItemLoader extends PaginatedLoader implements LoaderInterface
30
{
31
    public const SUPPORTED_TYPE = 'slideshowItems';
32
33
    /**
34
     * @var MetaFactoryInterface
35
     */
36
    protected $metaFactory;
37
38
    /**
39
     * @var Context
40
     */
41
    protected $context;
42
43
    public function __construct(
44
        MetaFactoryInterface $metaFactory,
45
        Context $context
46
    ) {
47
        $this->metaFactory = $metaFactory;
48
        $this->context = $context;
49
    }
50
51
    public function load($type, $withParameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
52
    {
53
        $criteria = new Criteria();
54
55
        if (array_key_exists('article', $withParameters) && $withParameters['article'] instanceof Meta) {
56
            $article = $withParameters['article']->getValues();
57
        } elseif (isset($this->context->article)) {
58
            $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...
59
        } else {
60
            return false;
61
        }
62
63
        if (LoaderInterface::COLLECTION === $responseType) {
64
            $slideshow = null;
65
66
            if (array_key_exists('slideshow', $withParameters)
67
                && ($slideshowParam = $withParameters['slideshow']) instanceof Meta
68
                && $slideshowParam->getValues() instanceof SlideshowInterface) {
69
                $slideshow = $slideshowParam->getValues();
70
            }
71
72
            if (null === $slideshow) {
73
                $slideshowItems = new ArrayCollection();
74
                foreach ($article->getSlideshows() as $slideshow) {
75
                    foreach ($slideshow->getSlideshowItems() as $slideshowItem) {
76
                        $slideshowItems->add($slideshowItem);
77
                    }
78
                }
79
            } else {
80
                $slideshowItems = $slideshow->getSlideshowItems();
81
            }
82
83
            $criteria = $this->applyPaginationToCriteria($criteria, $withParameters);
84
85
            if (0 < \count($slideshowItems)) {
86
                $collectionCriteria = new \Doctrine\Common\Collections\Criteria(
87
                    null,
88
                    $criteria->get('order'),
89
                    $criteria->get('firstResult'),
90
                    $criteria->get('maxResults')
91
                );
92
93
                $count = $slideshowItems->count();
94
                $articleMedia = $slideshowItems->matching($collectionCriteria);
95
                $metaCollection = new MetaCollection();
96
                $metaCollection->setTotalItemsCount($count);
97
98
                foreach ($articleMedia as $media) {
99
                    $metaCollection->add($this->metaFactory->create($media));
100
                }
101
102
                return $metaCollection;
103
            }
104
        }
105
    }
106
107
    public function isSupported(string $type): bool
108
    {
109
        return self::SUPPORTED_TYPE === $type && $this->context->isPreviewMode();
110
    }
111
}
112