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

getOneOrNullByPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
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 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\Doctrine\ORM;
18
19
use SWP\Bundle\CoreBundle\Pagination\Paginator;
20
use SWP\Component\Common\Criteria\Criteria;
21
use SWP\Component\Common\Pagination\PaginationData;
22
use SWP\Component\ContentList\Model\ContentListInterface;
23
use SWP\Component\ContentList\Model\ContentListItemInterface;
24
use SWP\Component\ContentList\Repository\ContentListItemRepositoryInterface;
25
use SWP\Bundle\StorageBundle\Doctrine\ORM\SortableEntityRepository;
26
use SWP\Component\Storage\Repository\RepositoryInterface;
27
28
class ContentListItemRepository extends SortableEntityRepository implements ContentListItemRepositoryInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function removeItems(ContentListInterface $contentList)
34
    {
35
        $queryBuilder = $this->createQueryBuilder('i')
36
            ->delete()
37
            ->where('i.contentList = :contentList')
38
            ->setParameter('contentList', $contentList);
39
40
        $queryBuilder->getQuery()->execute();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getSortedItems(Criteria $criteria, array $sorting = [], array $groupValues = [])
47
    {
48
        $queryBuilder = parent::getBySortableGroupsQueryBuilder($groupValues);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getBySortableGroupsQueryBuilder() instead of getSortedItems()). Are you sure this is correct? If so, you might want to change this to $this->getBySortableGroupsQueryBuilder().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
49
        $this->applyCriteria($queryBuilder, $criteria, 'n');
50
        $queryBuilder->resetDQLPart('orderBy');
51
        $this->applySorting($queryBuilder, $sorting, 'n');
52
        $queryBuilder->addOrderBy('n.position');
53
        $this->applyLimiting($queryBuilder, $criteria);
54
55
        return $queryBuilder;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getPaginatedByCriteria(Criteria $criteria, array $sorting = [], PaginationData $paginationData = null)
62
    {
63
        $queryBuilder = $this->getSortedItems($criteria, $sorting, ['contentList' => $criteria->get('contentList')]);
64
65
        if ($criteria->has('exclude_content')) {
66
            $excludeContent = $criteria->get('exclude_content');
67
            if (\is_numeric($excludeContent)) {
68
                $excludeContent = [$excludeContent];
69
            }
70
            $queryBuilder->andWhere($queryBuilder->expr()->notIn('n.content', $excludeContent));
71
        }
72
73
        if (null === $paginationData) {
74
            $paginator = new Paginator();
75
76
            return $paginator->paginate(
77
                $queryBuilder,
78
                $criteria->get('firstResult', 0),
79
                $criteria->get('maxResults', RepositoryInterface::MAX_RESULTS)
80
            );
81
        }
82
83
        return $this->getPaginator($queryBuilder, $paginationData);
84
    }
85
86
    public function getCountByCriteria(Criteria $criteria): int
87
    {
88
        return (int) $this->getQueryByCriteria($criteria, $criteria->get('order', []), 'n')
89
            ->select('COUNT(n.id)')
90
            ->setFirstResult(null)
91
            ->setMaxResults(null)
92
            ->getQuery()
93
            ->getSingleScalarResult();
94
    }
95
96
    public function getOneOrNullByPosition(Criteria $criteria, int $position): ?ContentListItemInterface
97
    {
98
        return $this->getQueryByCriteria($criteria, [], 'n')
99
            ->orderBy('n.position', 'DESC')
100
            ->andWhere('n.position = :position')->setParameter('position', $position)
101
            ->getQuery()
102
            ->setMaxResults(1)
103
            ->getOneOrNullResult();
104
    }
105
}
106