Lines: 73.08%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Component\Utility\Unicode; | |
| 8 | use Drupal\Core\Path\CurrentPathStack; | |
| 9 | use Drupal\Core\Routing\RouteMatchInterface; | |
| 10 | use Drupal\visitors\Helper\SiteSearchQuery; | |
| 11 | use Drupal\visitors\SiteSearch\SiteSearchHit; | |
| 12 | use Drupal\visitors\VisitorsSiteSearchDetectorInterface; | |
| 13 | use Drupal\visitors\VisitorsSiteSearchInterface; | |
| 14 | use Drupal\visitors\VisitorsSiteSearchPluginManager; | |
| 15 | use Symfony\Component\HttpFoundation\RequestStack; | |
| 16 | ||
| 17 | /** | |
| 18 | * Classifies a page view against the previous site search. | |
| 19 | */ | |
| 20 | final class SiteSearchService implements VisitorsSiteSearchDetectorInterface { | |
| 21 | ||
| 22 | /** | |
| 23 | * Constructs a site search service. | |
| 24 | * | |
| 25 | * @param \Drupal\visitors\VisitorsSiteSearchPluginManager $pluginManager | |
| 26 | * The site search plugin manager. | |
| 27 | * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch | |
| 28 | * The current route match. | |
| 29 | * @param \Drupal\Core\Path\CurrentPathStack $currentPath | |
| 30 | * The current path. | |
| 31 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack | |
| 32 | * The request stack. | |
| 33 | */ | |
| 34 | public function __construct( | |
| 35 | protected VisitorsSiteSearchPluginManager $pluginManager, | |
| 36 | protected RouteMatchInterface $routeMatch, | |
| 37 | protected CurrentPathStack $currentPath, | |
| 38 | protected RequestStack $requestStack, | |
| 39 | 11 | ) {} |
| 40 | ||
| 41 | /** | |
| 42 | * {@inheritdoc} | |
| 43 | */ | |
| 44 | public function eventFields( | |
| 45 | string $route, | |
| 46 | string $path, | |
| 47 | string $url, | |
| 48 | string $referrerRoute, | |
| 49 | string $referrerPath, | |
| 50 | string $referrerUrl, | |
| 51 | array $beacon, | |
| 52 | ): ?array { | |
| 53 | 11 | $current = $this->detect($route, $path, $url, $beacon, TRUE); |
| 54 | 11 | if ($current === NULL) { |
| 55 | 7 | return NULL; |
| 56 | } | |
| 57 | 5 | $previous = $this->detect($referrerRoute, $referrerPath, $referrerUrl, [], FALSE); |
| 58 | 5 | $current = $this->limit($current); |
| 59 | 5 | if ($previous !== NULL) { |
| 60 | 3 | $previous = $this->limit($previous); |
| 61 | } | |
| 62 | ||
| 63 | 5 | return [ |
| 64 | 5 | 'event_category' => 'Search', |
| 65 | 5 | 'event_action' => $this->action($current, $previous), |
| 66 | 5 | 'event_name' => $current->keyword, |
| 67 | 5 | 'event_value' => $this->count($beacon), |
| 68 | 5 | 'plugin_int_1' => $current->page, |
| 69 | 5 | 'plugin_var_1' => $current->filters, |
| 70 | 5 | 'plugin_var_2' => $previous?->keyword, |
| 71 | 5 | 'plugin_int_2' => $previous?->page, |
| 72 | 5 | 'plugin' => $current->pluginId, |
| 73 | 5 | ]; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * {@inheritdoc} | |
| 78 | */ | |
| 79 | public function resultCount(): ?int { | |
| 80 | 0 | $request = $this->requestStack->getCurrentRequest(); |
| 81 | 0 | $route = $this->routeMatch->getRouteName() ?? ''; |
| 82 | 0 | $path = $this->currentPath->getPath(); |
| 83 | 0 | $query = $request?->query->all() ?? []; |
| 84 | 0 | $plugin = $this->matchingPlugin($route, $path, $query); |
| 85 | 0 | if (!$plugin instanceof VisitorsSiteSearchInterface) { |
| 86 | 0 | return NULL; |
| 87 | } | |
| 88 | 0 | return $plugin->resultCount(); |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * {@inheritdoc} | |
| 93 | */ | |
| 94 | public function tracksView(string $viewId, string $displayId): bool { | |
| 95 | 0 | if ($viewId === '' || $displayId === '') { |
| 96 | 0 | return FALSE; |
| 97 | } | |
| 98 | 0 | return $this->matchingPlugin('view.' . $viewId . '.' . $displayId, '', []) !== NULL; |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Detects a search on a URL. | |
| 103 | * | |
| 104 | * The first detector that applies owns the URL. An empty keyword is not a | |
| 105 | * search, and a later detector is not tried. A beacon keyword replaces the | |
| 106 | * keyword parsed from the URL. | |
| 107 | * | |
| 108 | * @param string $route | |
| 109 | * The Drupal route name. | |
| 110 | * @param string $path | |
| 111 | * The Drupal path. | |
| 112 | * @param string $url | |
| 113 | * The URL, including the query string. | |
| 114 | * @param array $beacon | |
| 115 | * The tracking query. | |
| 116 | * @param bool $useBeacon | |
| 117 | * Whether the search beacon parameter may supply the keyword. | |
| 118 | * | |
| 119 | * @return \Drupal\visitors\SiteSearch\SiteSearchHit|null | |
| 120 | * The search, or NULL. | |
| 121 | */ | |
| 122 | private function detect(string $route, string $path, string $url, array $beacon, bool $useBeacon): ?SiteSearchHit { | |
| 123 | 11 | if ($path === '') { |
| 124 | 8 | $path = SiteSearchQuery::pathFromUrl($url); |
| 125 | } | |
| 126 | 11 | $query = SiteSearchQuery::fromUrl($url); |
| 127 | 11 | $beacon_keyword = $useBeacon ? SiteSearchQuery::scalar($beacon['search'] ?? NULL) : ''; |
| 128 | 11 | foreach ($this->plugins() as $plugin) { |
| 129 | 11 | if (!$plugin->applies($route, $path, $query)) { |
| 130 | 0 | continue; |
| 131 | } | |
| 132 | 6 | $parsed = $plugin->parse($route, $path, $query); |
| 133 | 6 | if ($beacon_keyword === '') { |
| 134 | 5 | return $parsed; |
| 135 | } | |
| 136 | 1 | return new SiteSearchHit( |
| 137 | 1 | $parsed?->pluginId ?? $plugin->getPluginId(), |
| 138 | 1 | $beacon_keyword, |
| 139 | 1 | $parsed?->page ?? SiteSearchQuery::page($query), |
| 140 | 1 | $parsed?->filters ?? SiteSearchQuery::canonical($query, [ |
| 141 | 1 | 'page', |
| 142 | 1 | 'search', |
| 143 | 1 | 'search_cat', |
| 144 | 1 | 'search_count', |
| 145 | 1 | ]), |
| 146 | 1 | ); |
| 147 | } | |
| 148 | 10 | if ($beacon_keyword === '') { |
| 149 | 10 | return NULL; |
| 150 | } | |
| 151 | 0 | return new SiteSearchHit( |
| 152 | 0 | 'search', |
| 153 | 0 | $beacon_keyword, |
| 154 | 0 | SiteSearchQuery::page($query), |
| 155 | 0 | SiteSearchQuery::canonical($query, [ |
| 156 | 0 | 'page', |
| 157 | 0 | 'search', |
| 158 | 0 | 'search_cat', |
| 159 | 0 | 'search_count', |
| 160 | 0 | ]), |
| 161 | 0 | ); |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * Classifies the current search against the referrer search. | |
| 166 | * | |
| 167 | * @param \Drupal\visitors\SiteSearch\SiteSearchHit $current | |
| 168 | * The search on this page. | |
| 169 | * @param \Drupal\visitors\SiteSearch\SiteSearchHit|null $previous | |
| 170 | * The search on the referrer, or NULL. | |
| 171 | * | |
| 172 | * @return string | |
| 173 | * Search, Next, Previous, Filter, or Keyword. | |
| 174 | */ | |
| 175 | private function action(SiteSearchHit $current, ?SiteSearchHit $previous): string { | |
| 176 | 5 | if ($previous === NULL) { |
| 177 | 3 | return 'Search'; |
| 178 | } | |
| 179 | 3 | $same_keyword = $current->keyword === $previous->keyword; |
| 180 | 3 | $same_filters = $current->filters === $previous->filters; |
| 181 | 3 | if ($same_keyword && $same_filters && $current->page > $previous->page) { |
| 182 | 1 | return 'Next'; |
| 183 | } | |
| 184 | 3 | if ($same_keyword && $same_filters && $current->page < $previous->page) { |
| 185 | 2 | return 'Previous'; |
| 186 | } | |
| 187 | 2 | if ($same_keyword && $current->page === 0 && !$same_filters) { |
| 188 | 2 | return 'Filter'; |
| 189 | } | |
| 190 | 1 | if (!$same_keyword && $same_filters) { |
| 191 | 1 | return 'Keyword'; |
| 192 | } | |
| 193 | 1 | return 'Search'; |
| 194 | } | |
| 195 | ||
| 196 | /** | |
| 197 | * Cuts the keyword and filters to the column length. | |
| 198 | * | |
| 199 | * @param \Drupal\visitors\SiteSearch\SiteSearchHit $hit | |
| 200 | * The detected search. | |
| 201 | * | |
| 202 | * @return \Drupal\visitors\SiteSearch\SiteSearchHit | |
| 203 | * The search that will be stored. | |
| 204 | */ | |
| 205 | private function limit(SiteSearchHit $hit): SiteSearchHit { | |
| 206 | 5 | return new SiteSearchHit( |
| 207 | 5 | $hit->pluginId, |
| 208 | 5 | Unicode::truncate($hit->keyword, 255, FALSE, FALSE), |
| 209 | 5 | $hit->page, |
| 210 | 5 | Unicode::truncate($hit->filters, 255, FALSE, FALSE), |
| 211 | 5 | ); |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * Result count sent on the tracking request. | |
| 216 | * | |
| 217 | * @param array $beacon | |
| 218 | * The tracking query. | |
| 219 | * | |
| 220 | * @return float|null | |
| 221 | * The count, or NULL when it was not sent. | |
| 222 | */ | |
| 223 | private function count(array $beacon): ?float { | |
| 224 | 5 | $value = $beacon['search_count'] ?? NULL; |
| 225 | 5 | if (!is_scalar($value)) { |
| 226 | 4 | return NULL; |
| 227 | } | |
| 228 | 2 | $value = trim((string) $value); |
| 229 | 2 | if ($value === '' || !is_numeric($value) || (float) $value < 0) { |
| 230 | 0 | return NULL; |
| 231 | } | |
| 232 | 2 | return (float) $value; |
| 233 | } | |
| 234 | ||
| 235 | /** | |
| 236 | * First detector that owns this route. | |
| 237 | * | |
| 238 | * @param string $route | |
| 239 | * The Drupal route name. | |
| 240 | * @param string $path | |
| 241 | * The Drupal path. | |
| 242 | * @param array $query | |
| 243 | * The query parameters. | |
| 244 | * | |
| 245 | * @return \Drupal\visitors\VisitorsSiteSearchInterface|null | |
| 246 | * The detector, or NULL. | |
| 247 | */ | |
| 248 | private function matchingPlugin(string $route, string $path, array $query): ?VisitorsSiteSearchInterface { | |
| 249 | 0 | foreach ($this->plugins() as $plugin) { |
| 250 | 0 | if ($plugin->applies($route, $path, $query)) { |
| 251 | 0 | return $plugin; |
| 252 | } | |
| 253 | } | |
| 254 | 0 | return NULL; |
| 255 | } | |
| 256 | ||
| 257 | /** | |
| 258 | * Detectors in weight order. | |
| 259 | * | |
| 260 | * @return \Drupal\visitors\VisitorsSiteSearchInterface[] | |
| 261 | * The detectors. | |
| 262 | */ | |
| 263 | private function plugins(): array { | |
| 264 | 11 | $definitions = $this->pluginManager->getDefinitions(); |
| 265 | 11 | uasort($definitions, static function (array $left, array $right): int { |
| 266 | 11 | return ($left['weight'] ?? 0) <=> ($right['weight'] ?? 0); |
| 267 | 11 | }); |
| 268 | 11 | $plugins = []; |
| 269 | 11 | foreach (array_keys($definitions) as $id) { |
| 270 | 11 | $plugin = $this->pluginManager->createInstance($id); |
| 271 | 11 | if ($plugin instanceof VisitorsSiteSearchInterface) { |
| 272 | 11 | $plugins[] = $plugin; |
| 273 | } | |
| 274 | } | |
| 275 | 11 | return $plugins; |
| 276 | } | |
| 277 | ||
| 278 | } |