Passed
Push — master ( 9a88de...e5ef09 )
by Timo
21:32
created

LastSearchesService::getLastSearches()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 3
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015-2016 Timo Schmidt <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\System\Session\FrontendUserSession;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Persistence\Generic\Session;
32
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
33
34
/**
35
 * The LastSearchesService is responsible to return the LastSearches from the session or database,
36
 * depending on the configuration.
37
 *
38
 * @author Timo Schmidt <[email protected]>
39
 */
40
class LastSearchesService
41
{
42
43
    /**
44
     * @var TypoScriptConfiguration
45
     */
46
    protected $configuration;
47
48
    /**
49
     * @var FrontendUserSession
50
     */
51
    protected $session;
52
53
    /**
54
     * @var LastSearchesRepository
55
     */
56
    protected $lastSearchesRepository;
57
58
    /**
59
     * @param TypoScriptConfiguration $typoscriptConfiguration
60
     * @param FrontendUserSession|null $session
61
     * @param LastSearchesRepository|null $lastSearchesRepository
62
     */
63 35
    public function __construct(TypoScriptConfiguration $typoscriptConfiguration, FrontendUserSession $session = null, LastSearchesRepository $lastSearchesRepository = null)
64
    {
65 35
        $this->configuration = $typoscriptConfiguration;
66 35
        $this->session = isset($session) ? $session : GeneralUtility::makeInstance(FrontendUserSession::class);
67 35
        $this->lastSearchesRepository = isset($lastSearchesRepository) ? $lastSearchesRepository : GeneralUtility::makeInstance(LastSearchesRepository::class);
68 35
    }
69
70
    /**
71
     * Retrieves the last searches from the session or database depending on the configuration.
72
     *
73
     * @return array
74
     */
75 34
    public function getLastSearches()
76
    {
77 34
        $lastSearchesKeywords = [];
78 34
        $mode   = $this->configuration->getSearchLastSearchesMode();
79 34
        $limit  = $this->configuration->getSearchLastSearchesLimit();
80
81
        switch ($mode) {
82 34
            case 'user':
83 31
                $lastSearchesKeywords = $this->getLastSearchesFromSession($limit);
84 31
                break;
85 3
            case 'global':
86 3
                $lastSearchesKeywords = $this->lastSearchesRepository->findAllKeywords($limit);
87 3
                break;
88
        }
89
90 34
        return $lastSearchesKeywords;
91
    }
92
93
    /**
94
     * Saves the keywords to the last searches in the database or session depending on the configuration.
95
     *
96
     * @param string $keywords
97
     * @throws \UnexpectedValueException
98
     */
99 24
    public function addToLastSearches($keywords)
100
    {
101 24
        $mode = $this->configuration->getSearchLastSearchesMode();
102
        switch ($mode) {
103 24
            case 'user':
104 23
                $this->storeKeywordsToSession($keywords);
105 23
                break;
106 1
            case 'global':
107 1
                $this->lastSearchesRepository->add($keywords, (int)$this->configuration->getSearchLastSearchesLimit());
108 1
                break;
109
            default:
110
                throw new \UnexpectedValueException(
111
                    'Unknown mode for plugin.tx_solr.search.lastSearches.mode, valid modes are "user" or "global".',
112
                    1342456570
113
                );
114
        }
115 24
    }
116
117
    /**
118
     * Gets the last searched keywords from the user's session
119
     *
120
     * @param int $limit
121
     * @return array An array containing the last searches of the current user
122
     */
123 31
    protected function getLastSearchesFromSession($limit)
124
    {
125 31
        $lastSearches = $this->session->getLastSearches();
126 31
        $lastSearches = array_slice(array_reverse(array_unique($lastSearches)), 0, $limit);
127
128 31
        return $lastSearches;
129
    }
130
131
    /**
132
     * Stores the keywords from the current query to the user's session.
133
     *
134
     * @param string $keywords The current query's keywords
135
     * @return void
136
     */
137 23
    protected function storeKeywordsToSession($keywords)
138
    {
139 23
        $currentLastSearches = $this->session->getLastSearches();
140 23
        $lastSearches = $currentLastSearches;
141 23
        $newLastSearchesCount = array_push($lastSearches, $keywords);
142
143 23
        while ($newLastSearchesCount > $this->configuration->getSearchLastSearchesLimit()) {
144
            array_shift($lastSearches);
145
            $newLastSearchesCount = count($lastSearches);
146
        }
147
148 23
        $this->session->setLastSearches($lastSearches);
149 23
    }
150
}
151