Completed
Push — master ( 42db1b...a35559 )
by Paweł
16s
created

ContentListsItemLoader::isSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content List Bundle.
7
 *
8
 * Copyright 2016 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 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentListBundle\Loader;
18
19
use Doctrine\Common\Collections\ArrayCollection;
20
use SWP\Bundle\ContentBundle\Loader\PaginatedLoader;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Component\ContentList\Repository\ContentListItemRepositoryInterface;
23
use SWP\Component\ContentList\Repository\ContentListRepositoryInterface;
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
/**
30
 * Class ContentListsItemLoader.
31
 */
32
class ContentListsItemLoader extends PaginatedLoader implements LoaderInterface
33
{
34
    /**
35
     * @var ContentListRepositoryInterface
36
     */
37
    protected $contentListRepository;
38
39
    /**
40
     * @var ContentListItemRepositoryInterface
41
     */
42
    protected $contentListItemsRepository;
43
44
    /**
45
     * @var MetaFactoryInterface
46
     */
47
    protected $metaFactory;
48
49
    /**
50
     * ContentListsItemLoader constructor.
51
     *
52
     * @param ContentListRepositoryInterface     $contentListRepository
53
     * @param ContentListItemRepositoryInterface $contentListItemRepository
54
     */
55
    public function __construct(
56
        ContentListRepositoryInterface $contentListRepository,
57
        ContentListItemRepositoryInterface $contentListItemRepository,
58
        MetaFactoryInterface $metaFactory
59
    ) {
60
        $this->contentListRepository = $contentListRepository;
61
        $this->contentListItemsRepository = $contentListItemRepository;
62
        $this->metaFactory = $metaFactory;
63
    }
64
65
    /**
66
     * Load meta object by provided type and parameters.
67
     *
68
     * @MetaLoaderDoc(
69
     *     description="Content List Item Loader loads Content List Items from Content List",
70
     *     parameters={
71
     *         listName="COLLECTION|name of content list"
72
     *     }
73
     * )
74
     *
75
     * @param string $type         object type
76
     * @param array  $parameters   parameters needed to load required object type
77
     * @param int    $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
78
     *
79
     * @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise
80
     *
81
     * @throws \Exception
82
     */
83
    public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE)
84
    {
85
        $criteria = new Criteria();
86
        if ($responseType === LoaderInterface::COLLECTION) {
87
            if (array_key_exists('contentListId', $parameters) && is_numeric($parameters['contentListId'])) {
88
                $contentList = $this->contentListRepository->findOneBy(['id' => $parameters['contentListId']]);
89
                $criteria->set('contentList', $contentList);
90
            } elseif (array_key_exists('contentListName', $parameters) && is_string($parameters['contentListName'])) {
91
                $contentList = $this->contentListRepository->findOneBy(['name' => $parameters['contentListName']]);
92
                $criteria->set('contentList', $contentList);
93
            }
94
95
            if (!$criteria->has('contentList')) {
96
                return false;
97
            }
98
99
            if (array_key_exists('sticky', $parameters) && is_bool($parameters['sticky'])) {
100
                $criteria->set('sticky', $parameters['sticky']);
101
            }
102
103
            $criteria = $this->applyPaginationToCriteria($criteria, $parameters);
104
            $contentListItems = $this->contentListItemsRepository->getPaginatedByCriteria($criteria, $criteria->get('order', [
105
                'sticky' => 'desc',
106
            ]));
107
            $itemsCollection = new ArrayCollection($contentListItems->getItems());
108
            if ($itemsCollection->count() > 0) {
109
                $metaCollection = new MetaCollection();
110
                $metaCollection->setTotalItemsCount($contentListItems->getTotalItemCount());
111
                foreach ($itemsCollection as $item) {
112
                    $itemMeta = $this->getItemMeta($item);
113
                    if (null !== $itemMeta) {
114
                        $metaCollection->add($itemMeta);
115
                    }
116
                }
117
                unset($itemsCollection, $criteria);
118
119
                return $metaCollection;
120
            }
121
        }
122
    }
123
124
    /**
125
     * Checks if Loader supports provided type.
126
     *
127
     * @param string $type
128
     *
129
     * @return bool
130
     */
131
    public function isSupported(string $type): bool
132
    {
133
        return in_array($type, ['contentListItems']);
134
    }
135
136
    private function getItemMeta($item)
137
    {
138
        if (null !== $item) {
139
            return $this->metaFactory->create($item);
140
        }
141
142
        return;
143
    }
144
}
145