Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

AdminContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 26
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setAdmin() 0 7 2
A getAdmin() 0 7 2
A hasAdmin() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Admin\Helper;
6
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\Exception\Exception;
9
10
class AdminContext implements AdminContextInterface
11
{
12
    private bool $frozen = false;
13
    private AdminInterface $admin;
14
15
    public function setAdmin(AdminInterface $admin): void
16
    {
17
        if ($this->frozen) {
18
            throw new Exception('The current admin cannot be set twice in a request');
19
        }
20
        $this->admin = $admin;
21
        $this->frozen = true;
22
    }
23
24
    public function getAdmin(): AdminInterface
25
    {
26
        if (!$this->hasAdmin()) {
27
            throw new Exception('No admin has been set yet.');
28
        }
29
30
        return $this->admin;
31
    }
32
33
    public function hasAdmin(): bool
34
    {
35
        return isset($this->admin);
36
    }
37
}
38