Coverage index

src/Service/CookieService.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Service;
4
5
use Drupal\visitors\VisitorsCookieInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
/**
9
 * Cookie service.
10
 */
11
class CookieService implements VisitorsCookieInterface {
12
13
  /**
14
   * The request object.
15
   *
16
   * @var \Symfony\Component\HttpFoundation\Request
17
   */
18
  protected $request;
19
20
  /**
21
   * Constructs a new CookieService.
22
   *
23
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
24
   *   The request object.
25
   */
26
  public function __construct(RequestStack $request_stack) {
2726
    $this->request = $request_stack->getCurrentRequest();
28
  }
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function getId(): ?string {
3413
    $_pk_id = NULL;
3513
    foreach ($this->request->cookies as $name => $value) {
3612
      if (strpos($name, '_pk_id_') === 0) {
3711
        $_pk_id = $value;
38
      }
39
    }
4013
    [$visitor_id] = is_string($_pk_id) ? explode('.', $_pk_id) : [NULL];
41
4213
    return $visitor_id;
43
  }
44
45
}