Completed
Push — master ( 009411...7fe99b )
by Rafał
18:49 queued 07:10
created

ContentListService::repositionStickyItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
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\Services;
18
19
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
20
use SWP\Bundle\ContentListBundle\Event\ContentListEvent;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Component\ContentList\ContentListEvents;
23
use SWP\Component\ContentList\Model\ContentListInterface;
24
use SWP\Component\ContentList\Model\ContentListItemInterface;
25
use SWP\Component\ContentList\Model\ListContentInterface;
26
use SWP\Component\ContentList\Repository\ContentListItemRepositoryInterface;
27
use SWP\Component\Storage\Factory\FactoryInterface;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
30
final class ContentListService implements ContentListServiceInterface
31
{
32
    private $eventDispatcher;
33
34
    private $listItemFactory;
35
36
    private $contentListItemRepository;
37
38
    public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $listItemFactory, ContentListItemRepositoryInterface $contentListItemRepository)
39
    {
40
        $this->eventDispatcher = $eventDispatcher;
41
        $this->listItemFactory = $listItemFactory;
42
        $this->contentListItemRepository = $contentListItemRepository;
43
    }
44
45
    public function addArticleToContentList(ContentListInterface $contentList, ArticleInterface $article, $position = null, bool $isSticky = false): ContentListItemInterface
46
    {
47
        /* @var ContentListItemInterface $contentListItem */
48
        $contentListItem = $this->listItemFactory->create();
49
50
        if ($article instanceof ListContentInterface) {
51
            $contentListItem->setContent($article);
52
        }
53
54
        if (null === $position) {
55
            $position = $contentList->getItems()->count();
0 ignored issues
show
Bug introduced by
The method getItems() does not seem to exist on object<SWP\Component\Con...l\ContentListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        }
57
58
        $contentListItem->setPosition((int) $position);
59
        $contentListItem->setContentList($contentList);
60
        $this->contentListItemRepository->add($contentListItem);
61
62
        $this->eventDispatcher->dispatch(
63
            ContentListEvents::POST_ITEM_ADD,
64
            new ContentListEvent($contentList, $contentListItem)
65
        );
66
        $contentList->setUpdatedAt(new \DateTime());
67
68
        $this->toggleStickOnItemPosition($contentListItem, $isSticky, (int) $position);
69
70
        return $contentListItem;
71
    }
72
73
    public function removeListItemsAboveTheLimit(ContentListInterface $contentList): void
74
    {
75
        $items = $this->contentListItemRepository
76
            ->getSortedItems(new Criteria(['contentList' => $contentList]), [], ['contentList' => $contentList])
77
            ->setMaxResults(null)
78
            ->getQuery()
79
            ->getResult();
80
81
        if (null !== $contentList->getLimit() && $contentList->getLimit() > 0 && \count($items) > $contentList->getLimit()) {
82
            foreach ($items as $key => $item) {
83
                if ($key + 1 > $contentList->getLimit()) {
84
                    $this->contentListItemRepository->remove($item);
85
                }
86
            }
87
        }
88
    }
89
90
    public function toggleStickOnItemPosition(ContentListItemInterface $contentListItem, bool $isSticky, int $position): void
91
    {
92
        $contentListItem->setSticky($isSticky);
93
94
        if ($contentListItem->isSticky()) {
95
            $contentListItem->setStickyPosition($position);
0 ignored issues
show
Bug introduced by
The method setStickyPosition() does not exist on SWP\Component\ContentLis...ontentListItemInterface. Did you maybe mean setSticky()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
96
        }
97
    }
98
99
    public function repositionStickyItems(ContentListInterface $contentList): void
100
    {
101
        $stickyItems = $this->contentListItemRepository->findBy([
102
            'sticky' => true,
103
            'contentList' => $contentList,
104
        ]);
105
106
        foreach ($stickyItems as $stickyItem) {
107
            if (null !== $stickyItem->getStickyPosition()) {
108
                $stickyItem->setPosition($stickyItem->getStickyPosition());
109
            }
110
        }
111
    }
112
113
    public function isAnyItemPinnedOnPosition(int $listId, int $position): ?ContentListItemInterface
114
    {
115
        return $this->contentListItemRepository->findOneBy([
116
            'contentList' => $listId,
117
            'position' => $position,
118
        ]);
119
    }
120
}
121