Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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) { | |
| 27 | 26 | $this->request = $request_stack->getCurrentRequest(); |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * {@inheritdoc} | |
| 32 | */ | |
| 33 | public function getId(): ?string { | |
| 34 | 13 | $_pk_id = NULL; |
| 35 | 13 | foreach ($this->request->cookies as $name => $value) { |
| 36 | 12 | if (strpos($name, '_pk_id_') === 0) { |
| 37 | 11 | $_pk_id = $value; |
| 38 | } | |
| 39 | } | |
| 40 | 13 | [$visitor_id] = is_string($_pk_id) ? explode('.', $_pk_id) : [NULL]; |
| 41 | ||
| 42 | 13 | return $visitor_id; |
| 43 | } | |
| 44 | ||
| 45 | } |