Completed
Push — 1.5 ( c5dc3b...b0c0ab )
by Paweł
19s
created

ContentListsItemLoader   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 147
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 11
dl 14
loc 147
rs 9.44
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D load() 14 85 33
A isSupported() 0 4 1
A getItemMeta() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Bundle\ContentBundle\Model\ArticleInterface;
22
use SWP\Component\Common\Criteria\Criteria;
23
use SWP\Component\ContentList\Model\ContentListInterface;
24
use SWP\Component\ContentList\Model\ContentListItemInterface;
25
use SWP\Component\ContentList\Repository\ContentListItemRepositoryInterface;
26
use SWP\Component\ContentList\Repository\ContentListRepositoryInterface;
27
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
28
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
29
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
30
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
31
32
/**
33
 * Class ContentListsItemLoader.
34
 */
35
class ContentListsItemLoader extends PaginatedLoader implements LoaderInterface
36
{
37
    /**
38
     * @var ContentListRepositoryInterface
39
     */
40
    protected $contentListRepository;
41
42
    /**
43
     * @var ContentListItemRepositoryInterface
44
     */
45
    protected $contentListItemsRepository;
46
47
    /**
48
     * @var MetaFactoryInterface
49
     */
50
    protected $metaFactory;
51
52
    /**
53
     * ContentListsItemLoader constructor.
54
     *
55
     * @param ContentListRepositoryInterface     $contentListRepository
56
     * @param ContentListItemRepositoryInterface $contentListItemRepository
57
     * @param MetaFactoryInterface               $metaFactory
58
     */
59
    public function __construct(
60
        ContentListRepositoryInterface $contentListRepository,
61
        ContentListItemRepositoryInterface $contentListItemRepository,
62
        MetaFactoryInterface $metaFactory
63
    ) {
64
        $this->contentListRepository = $contentListRepository;
65
        $this->contentListItemsRepository = $contentListItemRepository;
66
        $this->metaFactory = $metaFactory;
67
    }
68
69
    /**
70
     *  {@inheritdoc}
71
     */
72
    public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
73
    {
74
        $criteria = new Criteria();
75
        if (LoaderInterface::COLLECTION === $responseType) {
76
            if (array_key_exists('contentListId', $parameters) && is_numeric($parameters['contentListId'])) {
77
                $contentList = $this->contentListRepository->findOneBy(['id' => $parameters['contentListId']]);
78
                $criteria->set('contentList', $contentList);
79
            } elseif (array_key_exists('contentListName', $parameters) && is_string($parameters['contentListName'])) {
80
                $contentList = $this->contentListRepository->findOneBy(['name' => $parameters['contentListName']]);
81
                $criteria->set('contentList', $contentList);
82 View Code Duplication
            } elseif (
0 ignored issues
show
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...
83
                array_key_exists('contentList', $parameters) &&
84
                $parameters['contentList'] instanceof Meta &&
85
                $parameters['contentList']->getValues() instanceof ContentListInterface
86
            ) {
87
                $criteria->set('contentList', $parameters['contentList']->getValues());
88
            }
89
90
            if (!$criteria->has('contentList')) {
91
                return false;
92
            }
93
94
            if (array_key_exists('sticky', $parameters) && is_bool($parameters['sticky'])) {
95
                $criteria->set('sticky', $parameters['sticky']);
96
            }
97
98
            if (isset($withoutParameters['content']) && !empty($withoutParameters['content'])) {
99
                $criteria->set('exclude_content', $withoutParameters['content']);
100
            }
101
102
            $criteria = $this->applyPaginationToCriteria($criteria, $parameters);
103
            $contentListItems = $this->contentListItemsRepository->getPaginatedByCriteria($criteria, $criteria->get('order', [
104
                'sticky' => 'desc',
105
            ]));
106
            $itemsCollection = new ArrayCollection($contentListItems->getItems());
107
            if ($itemsCollection->count() > 0) {
108
                $metaCollection = new MetaCollection();
109
                $metaCollection->setTotalItemsCount($contentListItems->getTotalItemCount());
110
                foreach ($itemsCollection as $item) {
111
                    $itemMeta = $this->getItemMeta($item);
112
                    if (null !== $itemMeta) {
113
                        $metaCollection->add($itemMeta);
114
                    }
115
                }
116
                unset($itemsCollection, $criteria);
117
118
                return $metaCollection;
119
            }
120
        } elseif (LoaderInterface::SINGLE === $responseType) {
121
            if (array_key_exists('contentListName', $parameters) && is_string($parameters['contentListName'])) {
122
                $contentList = $this->contentListRepository->findOneBy(['name' => $parameters['contentListName']]);
123
                $criteria->set('contentList', $contentList);
124 View Code Duplication
            } elseif (
0 ignored issues
show
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...
125
                array_key_exists('contentList', $parameters) &&
126
                $parameters['contentList'] instanceof Meta &&
127
                $parameters['contentList']->getValues() instanceof ContentListInterface
128
            ) {
129
                $criteria->set('contentList', $parameters['contentList']->getValues());
130
            }
131
132
            if (
133
                isset($contentList)
134
                && array_key_exists('article', $parameters)
135
                && $parameters['article'] instanceof Meta
136
                && $parameters['article']->getValues() instanceof ArticleInterface
137
            ) {
138
                /** @var ContentListItemInterface $currentContentListItem */
139
                $currentContentListItem = $this->contentListItemsRepository->getQueryByCriteria(new Criteria([
140
                    'contentList' => $contentList,
141
                    'content' => $parameters['article']->getValues(),
142
                ]), [], 'n')->getQuery()->getOneOrNullResult();
143
                $position = $currentContentListItem->getPosition();
144
            }
145
146
            if (isset($position) && array_key_exists('prev', $parameters) && true === $parameters['prev']) {
147
                ++$position;
148
            } elseif (isset($position) && array_key_exists('next', $parameters) && true === $parameters['next']) {
149
                --$position;
150
            } else {
151
                return null;
152
            }
153
154
            return $this->getItemMeta($this->contentListItemsRepository->getOneOrNullByPosition($criteria, $position));
155
        }
156
    }
157
158
    /**
159
     * Checks if Loader supports provided type.
160
     *
161
     * @param string $type
162
     *
163
     * @return bool
164
     */
165
    public function isSupported(string $type): bool
166
    {
167
        return in_array($type, ['contentListItems', 'contentListItem']);
168
    }
169
170
    /**
171
     * @param mixed $item
172
     *
173
     * @return null|Meta
174
     */
175
    private function getItemMeta($item)
176
    {
177
        if (null !== $item) {
178
            return $this->metaFactory->create($item);
179
        }
180
    }
181
}
182