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

SearchResultRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 3 1
A __construct() 0 3 1
A persist() 0 3 1
A truncateTable() 0 7 1
A getResults() 0 23 5
A clearOldResults() 0 14 2
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\SearchResultRepositoryInterface;
20
use Zikula\SearchModule\Entity\SearchResultEntity;
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 results.
26
 */
27
class SearchResultRepository extends ServiceEntityRepository implements SearchResultRepositoryInterface
28
{
29
    public function __construct(ManagerRegistry $registry)
30
    {
31
        parent::__construct($registry, SearchResultEntity::class);
32
    }
33
34
    public function getResults(array $filters = [], array $sorting = [], int $page = 1, int $pageSize = 25): Paginator
35
    {
36
        $qb = $this->createQueryBuilder('tbl')
37
            ->select('tbl');
38
39
        // add clauses for where
40
        if (count($filters) > 0) {
41
            $i = 1;
42
            foreach ($filters as $w_key => $w_value) {
43
                $qb->andWhere($qb->expr()->eq('tbl.' . $w_key, '?' . $i))
44
                   ->setParameter($i, $w_value);
45
                $i++;
46
            }
47
        }
48
49
        // add clause for ordering
50
        if (count($sorting) > 0) {
51
            foreach ($sorting as $sort => $sortdir) {
52
                $qb->addOrderBy('tbl.' . $sort, $sortdir);
53
            }
54
        }
55
56
        return (new Paginator($qb, $pageSize))->paginate($page);
57
    }
58
59
    public function clearOldResults(string $sessionId = ''): void
60
    {
61
        $qb = $this->_em->createQueryBuilder()
62
            ->delete(SearchResultEntity::class, 'tbl')
63
            ->where('DATE_ADD(tbl.found, 1, \'DAY\') < CURRENT_TIMESTAMP()');
64
65
        if ('' !== $sessionId) {
66
            $qb->orWhere('tbl.sesid = :sid')
67
               ->setParameter('sid', $sessionId);
68
        }
69
70
        $query = $qb->getQuery();
71
72
        $query->execute();
73
    }
74
75
    public function persist(SearchResultEntity $entity): void
76
    {
77
        $this->_em->persist($entity);
78
    }
79
80
    public function flush(SearchResultEntity $entity = null): void
81
    {
82
        $this->_em->flush();
83
    }
84
85
    public function truncateTable(): void
86
    {
87
        $qb = $this->_em->createQueryBuilder()
88
            ->delete(SearchResultEntity::class, 'tbl');
89
        $query = $qb->getQuery();
90
91
        $query->execute();
92
    }
93
}
94