Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RebuildRouteService
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 rebuild
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
7
 getPaths
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Drupal\visitors\Service;
4
5use Drupal\Core\Database\Connection;
6use Drupal\Core\ParamConverter\ParamNotConvertedException;
7use Drupal\Core\Utility\Error;
8use Drupal\visitors\VisitorsRebuildRouteInterface;
9use Psr\Log\LoggerInterface;
10use Symfony\Component\HttpFoundation\Request;
11use Symfony\Component\Routing\Exception\ResourceNotFoundException;
12use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
13
14/**
15 * Service for rebuilding routes.
16 */
17class RebuildRouteService implements VisitorsRebuildRouteInterface {
18
19  /**
20   * The database connection.
21   *
22   * @var \Drupal\Core\Database\Connection
23   */
24  protected $database;
25
26  /**
27   * The router.
28   *
29   * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface
30   */
31  protected $routerMatcher;
32
33  /**
34   * The logger.
35   *
36   * @var \Psr\Log\LoggerInterface
37   */
38  protected $logger;
39
40  /**
41   * Constructs a new Rebuild Route Service.
42   *
43   * @param \Drupal\Core\Database\Connection $connection
44   *   The database connection.
45   * @param \Symfony\Component\Routing\Matcher\RequestMatcherInterface $router_matcher
46   *   The router matcher.
47   * @param \Psr\Log\LoggerInterface $logger
48   *   The logger.
49   */
50  public function __construct(Connection $connection, RequestMatcherInterface $router_matcher, LoggerInterface $logger) {
51    $this->database = $connection;
52    $this->routerMatcher = $router_matcher;
53    $this->logger = $logger;
54  }
55
56  /**
57   * {@inheritdoc}
58   */
59  public function rebuild(string $path): int {
60    $count = 0;
61    $route = '';
62    try {
63      $request = Request::create($path);
64      $result = $this->routerMatcher->matchRequest($request);
65      if (!empty($result['_route'])) {
66        $route = $result['_route'];
67      }
68
69    }
70    catch (ParamNotConvertedException $e) {
71      $route = $e->getRouteName();
72    }
73    catch (ResourceNotFoundException $e) {
74      // Do nothing.
75    }
76    catch (\Exception $e) {
77      Error::logException($this->logger, $e);
78    }
79
80    if (empty($route)) {
81      return $count;
82    }
83
84    try {
85      $count = $this->database->update('visitors')
86        ->fields(['route' => $route])
87        ->condition('visitors_path', $path)
88        ->execute();
89    }
90    catch (\Exception $e) {
91      Error::logException($this->logger, $e);
92    }
93
94    return $count;
95  }
96
97  /**
98   * {@inheritdoc}
99   */
100  public function getPaths(): array {
101    $records = [];
102    try {
103      $records = $this->database->select('visitors', 'v')
104        ->fields('v', ['visitors_path'])
105        ->condition('route', '')
106        ->distinct()
107        ->execute()
108        ->fetchAll();
109    }
110    catch (\Exception $e) {
111      Error::logException($this->logger, $e);
112    }
113
114    return $records;
115  }
116
117}