Passed
Push — master ( 9f83de...fb7724 )
by Timo
04:36
created

endSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
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 2 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 31
     * @param LastSearchesRepository|null $lastSearchesRepository
62
     */
63 31
    public function __construct(TypoScriptConfiguration $typoscriptConfiguration, FrontendUserSession $session = null, LastSearchesRepository $lastSearchesRepository = null)
64 31
    {
65 31
        $this->configuration = $typoscriptConfiguration;
66 31
        $this->session = isset($session) ? $session : GeneralUtility::makeInstance(FrontendUserSession::class);
67
        $this->lastSearchesRepository = isset($lastSearchesRepository) ? $lastSearchesRepository : GeneralUtility::makeInstance(LastSearchesRepository::class);
68
    }
69
70
    /**
71
     * Retrieves the last searches from the session or database depending on the configuration.
72
     *
73 30
     * @return array
74
     */
75 30
    public function getLastSearches()
76 30
    {
77 30
        $lastSearchesKeywords = [];
78
        $mode   = $this->configuration->getSearchLastSearchesMode();
79
        $limit  = $this->configuration->getSearchLastSearchesLimit();
80 30
81 27
        switch ($mode) {
82 27
            case 'user':
83 3
                $lastSearchesKeywords = $this->getLastSearchesFromSession($limit);
84 3
                break;
85 3
            case 'global':
86
                $lastSearchesKeywords = $this->lastSearchesRepository->findAllKeywords($limit);
87
                break;
88 30
        }
89
90
        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 20
     * @throws \UnexpectedValueException
98
     */
99 20
    public function addToLastSearches($keywords)
100
    {
101 20
        $mode = $this->configuration->getSearchLastSearchesMode();
102 19
        switch ($mode) {
103 19
            case 'user':
104 1
                $this->storeKeywordsToSession($keywords);
105 1
                break;
106 1
            case 'global':
107
                $this->lastSearchesRepository->add($keywords, (int)$this->configuration->getSearchLastSearchesLimit());
108
                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 20
                );
114
        }
115
    }
116
117
    /**
118
     * Gets the last searched keywords from the user's session
119
     *
120
     * @param int $limit
121 27
     * @return array An array containing the last searches of the current user
122
     */
123 27
    protected function getLastSearchesFromSession($limit)
124
    {
125 27
        $lastSearches = $this->session->getLastSearches();
126 8
        $lastSearches = array_slice(array_reverse(array_unique($lastSearches)), 0, $limit);
127
128
        return $lastSearches;
129 19
    }
130
131 19
    /**
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
    protected function storeKeywordsToSession($keywords)
138
    {
139
        $currentLastSearches = $this->session->getLastSearches();
140 3
        $lastSearches = $currentLastSearches;
141
        $newLastSearchesCount = array_push($lastSearches, $keywords);
142 3
143 3
        while ($newLastSearchesCount > $this->configuration->getSearchLastSearchesLimit()) {
144 3
            array_shift($lastSearches);
145 3
            $newLastSearchesCount = count($lastSearches);
146 3
        }
147 3
148
        $this->session->setLastSearches($lastSearches);
149
    }
150
}
151