Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ActiveHostProvider
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveHostId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveHost
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setActiveHostId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Shared\Host;
6
7use App\Domain\Host\Entity\HostRecord;
8use App\Domain\Host\Repository\HostRecordRepository;
9use Yiisoft\Session\SessionInterface;
10
11 class ActiveHostProvider
12{
13    private const SESSION_KEY = 'active_host_id';
14
15    public function __construct(
16        private SessionInterface $session,
17        private HostRecordRepository $hostRepository
18    ) {}
19
20    public function getActiveHostId(): ?string
21    {
22        /** @var string|null */
23        return $this->session->get(self::SESSION_KEY);
24    }
25
26    public function getActiveHost(): ?HostRecord
27    {
28        $id = $this->getActiveHostId();
29        if ($id === null) {
30            return null;
31        }
32
33        return $this->hostRepository->findById($id);
34    }
35
36    public function setActiveHostId(?string $hostId): void
37    {
38        if ($hostId === null) {
39            $this->session->remove(self::SESSION_KEY);
40        } else {
41            $this->session->set(self::SESSION_KEY, $hostId);
42        }
43    }
44}