1 | <?php |
||
38 | class LastSearchesService |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * @var TypoScriptConfiguration |
||
43 | */ |
||
44 | protected $configuration; |
||
45 | |||
46 | /** |
||
47 | * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController |
||
48 | */ |
||
49 | protected $tsfe; |
||
50 | |||
51 | /** |
||
52 | * @var LastSearchesRepository |
||
53 | */ |
||
54 | protected $lastSearchesRepository; |
||
55 | |||
56 | /** |
||
57 | * @param TypoScriptConfiguration $typoscriptConfiguration |
||
58 | * @param TypoScriptFrontendController $tsfe |
||
59 | * @param LastSearchesRepository|null $lastSearchesRepository |
||
60 | */ |
||
61 | 31 | public function __construct(TypoScriptConfiguration $typoscriptConfiguration, TypoScriptFrontendController $tsfe, LastSearchesRepository $lastSearchesRepository = null) |
|
62 | { |
||
63 | 31 | $this->configuration = $typoscriptConfiguration; |
|
64 | 31 | $this->tsfe = $tsfe; |
|
65 | 31 | $this->lastSearchesRepository = isset($lastSearchesRepository) ? $lastSearchesRepository : GeneralUtility::makeInstance(LastSearchesRepository::class); |
|
66 | 31 | } |
|
67 | |||
68 | /** |
||
69 | * Retrieves the last searches from the session or database depending on the configuration. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 30 | public function getLastSearches() |
|
90 | |||
91 | /** |
||
92 | * Saves the keywords to the last searches in the database or session depending on the configuration. |
||
93 | * |
||
94 | * @param string $keywords |
||
95 | * @throws \UnexpectedValueException |
||
96 | */ |
||
97 | 20 | public function addToLastSearches($keywords) |
|
98 | { |
||
99 | 20 | $mode = $this->configuration->getSearchLastSearchesMode(); |
|
100 | switch ($mode) { |
||
101 | 20 | case 'user': |
|
102 | 19 | $this->storeKeywordsToSession($keywords); |
|
103 | 19 | break; |
|
104 | 1 | case 'global': |
|
105 | 1 | $this->lastSearchesRepository->add($keywords, (int)$this->configuration->getSearchLastSearchesLimit()); |
|
106 | 1 | break; |
|
107 | default: |
||
108 | throw new \UnexpectedValueException( |
||
109 | 'Unknown mode for plugin.tx_solr.search.lastSearches.mode, valid modes are "user" or "global".', |
||
110 | 1342456570 |
||
111 | ); |
||
112 | } |
||
113 | 20 | } |
|
114 | |||
115 | /** |
||
116 | * Gets the last searched keywords from the user's session |
||
117 | * |
||
118 | * @param int $limit |
||
119 | * @return array An array containing the last searches of the current user |
||
120 | */ |
||
121 | 27 | protected function getLastSearchesFromSession($limit) |
|
133 | |||
134 | /** |
||
135 | * @return mixed |
||
136 | */ |
||
137 | protected function getLastSearchesFromFrontendSession() |
||
141 | |||
142 | 3 | /** |
|
143 | 3 | * Stores the keywords from the current query to the user's session. |
|
144 | 3 | * |
|
145 | 3 | * @param string $keywords The current query's keywords |
|
146 | 3 | * @return void |
|
147 | 3 | */ |
|
148 | protected function storeKeywordsToSession($keywords) |
||
170 | } |
||
171 |