Completed
Push — master ( 30f55a...646bd3 )
by Axel
06:07
created

SearchStatRepository::getStats()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 4
dl 0
loc 23
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\SearchModule\Entity\Repository;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Zikula\Bundle\CoreBundle\Doctrine\Paginator;
19
use Zikula\SearchModule\Entity\RepositoryInterface\SearchStatRepositoryInterface;
20
use Zikula\SearchModule\Entity\SearchStatEntity;
21
22
/**
23
 * Repository class used to implement own convenience methods for performing certain DQL queries.
24
 *
25
 * This is the repository class for search statistics.
26
 */
27
class SearchStatRepository extends ServiceEntityRepository implements SearchStatRepositoryInterface
28
{
29
    public function __construct(ManagerRegistry $registry)
30
    {
31
        parent::__construct($registry, SearchStatEntity::class);
32
    }
33
34
    public function countStats(): int
35
    {
36
        $qb = $this->createQueryBuilder('tbl')
37
            ->select('COUNT(tbl.id)');
38
39
        $query = $qb->getQuery();
40
41
        return (int)$query->getSingleScalarResult();
42
    }
43
44
    public function getStats(array $filters = [], array $sorting = [], int $page = 1, int $pageSize = 25): Paginator
45
    {
46
        $qb = $this->createQueryBuilder('tbl')
47
            ->select('tbl');
48
49
        // add clauses for where
50
        if (count($filters) > 0) {
51
            $i = 1;
52
            foreach ($filters as $w_key => $w_value) {
53
                $qb->andWhere($qb->expr()->eq('tbl.' . $w_key, '?' . $i))
54
                   ->setParameter($i, $w_value);
55
                $i++;
56
            }
57
        }
58
59
        // add clause for ordering
60
        if (count($sorting) > 0) {
61
            foreach ($sorting as $sort => $sortdir) {
62
                $qb->addOrderBy('tbl.' . $sort, $sortdir);
63
            }
64
        }
65
66
        return (new Paginator($qb, $pageSize))->paginate($page);
67
    }
68
69
    public function persistAndFlush(SearchStatEntity $entity): void
70
    {
71
        $this->_em->persist($entity);
72
        $this->_em->flush();
73
    }
74
}
75