Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

ArticleRepository::countByCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content 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\ContentBundle\Doctrine\ORM;
18
19
use Doctrine\ORM\QueryBuilder;
20
use SWP\Component\Common\Criteria\Criteria;
21
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
22
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
23
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository;
24
25
class ArticleRepository extends EntityRepository implements ArticleRepositoryInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 8
    public function findOneBySlug($slug)
31
    {
32 8
        return $this->findOneBy([
33 8
            'slug' => $slug,
34
        ]);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function findAllArticles()
41
    {
42
        return $this->getQueryByCriteria(new Criteria(), [], 'a')->getQuery()->getResult();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 12
    public function getByCriteria(Criteria $criteria, array $sorting): QueryBuilder
49
    {
50 12
        $qb = $this->getQueryByCriteria($criteria, $sorting, 'a');
51 12
        $qb->andWhere('a.status = :status')
52 12
            ->setParameter('status', $criteria->get('status', ArticleInterface::STATUS_PUBLISHED));
53
54 12
        return $qb;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function countByCriteria(Criteria $criteria): int
61
    {
62 2
        $qb = $this->createQueryBuilder('a')
63 2
            ->select('COUNT(a.id)')
64 2
            ->where('a.status = :status')
65 2
            ->setParameter('status', $criteria->get('status', ArticleInterface::STATUS_PUBLISHED));
66
67 2
        $this->applyCriteria($qb, $criteria, 'a');
68
69 2
        return (int) $qb->getQuery()->getSingleScalarResult();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 5
    public function findArticlesByCriteria(Criteria $criteria, array $sorting = []): array
76
    {
77 5
        $queryBuilder = $this->createQueryBuilder('a')
78 5
            ->where('a.status = :status')
79 5
            ->setParameter('status', $criteria->get('status', ArticleInterface::STATUS_PUBLISHED));
80
81 5
        if ($criteria->has('author')) {
82 2
            foreach ($criteria->get('author') as $author) {
83 2
                $queryBuilder->andWhere($queryBuilder->expr()->like('a.metadata', ':metadata'))
84 2
                    ->setParameter('metadata', '%'.$author.'%');
85
            }
86
87 2
            $criteria->remove('author');
88
        }
89
90 5
        if ($criteria->has('publishedBefore')) {
91
            $queryBuilder->andWhere('a.publishedAt < :before')
92
                ->setParameter('before', $criteria->get('publishedBefore'));
93
            $criteria->remove('publishedBefore');
94
        }
95
96 5
        if ($criteria->has('publishedAfter')) {
97
            $queryBuilder->andWhere('a.publishedAt > :after')
98
                ->setParameter('after', $criteria->get('publishedAfter'));
99
            $criteria->remove('publishedAfter');
100
        }
101
102 5
        if ($criteria->has('metadata')) {
103 3
            foreach ($criteria->get('metadata') as $key => $value) {
104 3
                $queryBuilder->andWhere($queryBuilder->expr()->like('a.metadata', ':'.$key))
105 3
                    ->setParameter($key, '%'.$value.'%');
106
            }
107
108 3
            $criteria->remove('metadata');
109
        }
110
111 5
        $this->applyCriteria($queryBuilder, $criteria, 'a');
112 5
        $this->applySorting($queryBuilder, $sorting, 'a');
113 5
        $this->applyLimiting($queryBuilder, $criteria);
114
115 5
        return $queryBuilder->getQuery()->getResult();
116
    }
117
118
    /**
119
     * @param string $identifier
120
     * @param array  $order
121
     *
122
     * @throws \Exception
123
     */
124
    public function getQueryForRouteArticles(string $identifier, array $order = [])
125
    {
126
        throw new \Exception('Not implemented');
127
    }
128
}
129