Completed
Push — 2.0 ( f46ccd...153639 )
by Paweł
10:00
created

ContentListService::removeListItemsAboveTheLimit()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 4
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): 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
        return $contentListItem;
69
    }
70
71
    public function removeListItemsAboveTheLimit(ContentListInterface $contentList): void
72
    {
73
        $items = $this->contentListItemRepository
74
            ->getSortedItems(new Criteria(['contentList' => $contentList]), [], ['contentList' => $contentList])
75
            ->getQuery()
76
            ->getResult();
77
78
        if (null !== $contentList->getLimit() && \count($items) > $contentList->getLimit()) {
79
            foreach ($items as $key => $item) {
80
                if ($key + 1 > $contentList->getLimit()) {
81
                    $this->contentListItemRepository->remove($item);
82
                }
83
            }
84
        }
85
    }
86
}
87