Passed
Push — release-11.5.x ( 385fe8...cd49eb )
by Rafael
53:22 queued 14:05
created

LastSearchesRepository::findAllKeywords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches;
17
18
use ApacheSolrForTypo3\Solr\System\Records\AbstractRepository;
19
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
20
use Doctrine\DBAL\Exception as DBALException;
21
use InvalidArgumentException;
22
use function json_encode;
23
24
class LastSearchesRepository extends AbstractRepository
25
{
26
    /**
27
     * @var string
28
     */
29
    protected string $table = 'tx_solr_last_searches';
30
31
    /**
32
     * Finds the last searched keywords from the database
33
     *
34
     * @param int $limit
35
     * @return array An array containing the last searches of the current user
36
     * @throws DBALDriverException
37
     * @throws DBALException|\Doctrine\DBAL\DBALException
38
     */
39 7
    public function findAllKeywords(int $limit = 10): array
40
    {
41 7
        $lastSearchesResultSet = $this->getLastSearchesResultSet($limit);
42 7
        if (empty($lastSearchesResultSet)) {
43 1
            return [];
44
        }
45
46 6
        $lastSearches = [];
47 6
        foreach ($lastSearchesResultSet as $row) {
48 6
            $lastSearches[] = html_entity_decode($row['keywords'], ENT_QUOTES, 'UTF-8');
49
        }
50
51 6
        return $lastSearches;
52
    }
53
54
    /**
55
     * Returns all last searches
56
     *
57
     * @param int $limit
58
     * @return array
59
     * @throws DBALException|\Doctrine\DBAL\DBALException
60
     * @throws DBALDriverException
61
     */
62 6
    protected function getLastSearchesResultSet(int $limit): array
63
    {
64 6
        $queryBuilder = $this->getQueryBuilder();
65 6
        return $queryBuilder
66 6
            ->select('keywords')
67 6
            ->addSelectLiteral(
68 6
                $queryBuilder->expr()->max('tstamp', 'maxtstamp')
69 6
            )
70 6
            ->from($this->table)
71 6
            // There is no support for DISTINCT, a ->groupBy() has to be used instead.
72 6
            ->groupBy('keywords')
73 6
            ->orderBy('maxtstamp', 'DESC')
74 6
            ->setMaxResults($limit)
75 6
            ->execute()
76 6
            ->fetchAllAssociative();
77
    }
78
79
    /**
80
     * Adds keywords to last searches or updates the oldest row by given limit.
81
     *
82
     * @param string $lastSearchesKeywords
83
     * @param int $lastSearchesLimit
84
     * @throws DBALDriverException
85
     * @throws DBALException|\Doctrine\DBAL\DBALException
86
     */
87 4
    public function add(string $lastSearchesKeywords, int $lastSearchesLimit)
88
    {
89 4
        $nextSequenceId = $this->resolveNextSequenceIdForGivenLimit($lastSearchesLimit);
90 4
        $rowsCount = $this->count();
91 4
        if ($nextSequenceId < $rowsCount) {
92 2
            $this->update([
93 2
                'sequence_id' => $nextSequenceId,
94 2
                'keywords' => $lastSearchesKeywords,
95 2
            ]);
96 2
            return;
97
        }
98
99 2
        $queryBuilder = $this->getQueryBuilder();
100 2
        $queryBuilder
101 2
            ->insert($this->table)
102 2
            ->values([
103 2
                'sequence_id' => $nextSequenceId,
104 2
                'keywords' => $lastSearchesKeywords,
105 2
                'tstamp' => time(),
106 2
            ])
107 2
            ->execute();
108
    }
109
110
    /**
111
     * Resolves next sequence id by given last searches limit.
112
     *
113
     * @param int $lastSearchesLimit
114
     * @return int
115
     * @throws DBALDriverException
116
     * @throws DBALException|\Doctrine\DBAL\DBALException
117
     */
118 4
    protected function resolveNextSequenceIdForGivenLimit(int $lastSearchesLimit): int
119
    {
120 4
        $nextSequenceId = 0;
121
122 4
        $queryBuilder = $this->getQueryBuilder();
123 4
        $result = $queryBuilder->select('sequence_id')
124 4
            ->from($this->table)
125 4
            ->orderBy('tstamp', 'DESC')
126 4
            ->setMaxResults(1)
127 4
            ->execute()
128 4
            ->fetchAssociative();
129
130 4
        if (!empty($result)) {
131 4
            $nextSequenceId = ($result['sequence_id'] + 1) % $lastSearchesLimit;
132
        }
133
134 4
        return $nextSequenceId;
135
    }
136
137
    /**
138
     * Updates last searches row by using sequence_id from given $lastSearchesRow array
139
     *
140
     * @param array $lastSearchesRow
141
     * @throws InvalidArgumentException
142
     * @throws DBALException|\Doctrine\DBAL\DBALException
143
     */
144 2
    protected function update(array $lastSearchesRow)
145
    {
146 2
        $queryBuilder = $this->getQueryBuilder();
147
148 2
        $affectedRows = $queryBuilder
149 2
            ->update($this->table)
150 2
            ->where(
151 2
                $queryBuilder->expr()->eq('sequence_id', $queryBuilder->createNamedParameter($lastSearchesRow['sequence_id']))
152 2
            )
153 2
            ->set('tstamp', time())
154 2
            ->set('keywords', $lastSearchesRow['keywords'])
155 2
            ->execute();
156
157 2
        if ($affectedRows < 1) {
158
            throw new InvalidArgumentException(vsprintf('By trying to update last searches row with values "%s" nothing was updated, make sure the given "sequence_id" exists in database.', [json_encode($lastSearchesRow)]), 1502717923);
159
        }
160
    }
161
}
162