Passed
Push — master ( 624a9a...4c85b9 )
by Arthur
36:47
created

GuardDispatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 32
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 8 4
A __construct() 0 4 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 24.03.19
6
 * Time: 19:24
7
 */
8
9
namespace Foundation\Guard\Dispatcher;
10
11
12
use Foundation\Exceptions\Exception;
13
use Foundation\Guard\Contracts\GuardContract;
14
use Foundation\Guard\Exceptions\CouldNotResolveStringException;
15
use Foundation\Guard\Resolvers\ExceptionResolver;
16
use Foundation\Guard\Resolvers\GuardsResolver;
17
use Symfony\Component\HttpFoundation\File\Exception\UnexpectedTypeException;
18
use Throwable;
19
20
class GuardDispatcher
21
{
22
23
    /**
24
     * @var GuardContract[]
25
     */
26
    protected $guards = [];
27
28
    /**
29
     * @var Throwable|null
30
     */
31
    protected $exception;
32
33
    /**
34
     * GuardDispatcher constructor.
35
     * @param $guards
36
     * @param $exception
37
     */
38 2
    public function __construct($guards, $exception = null)
39
    {
40 2
        $this->guards = (new GuardsResolver($guards))->resolve();
41 2
        $this->exception = isset($exception) ? (new ExceptionResolver($exception))->resolve() : null;
42 2
    }
43
44 2
    public function dispatch()
45
    {
46 2
        foreach ($this->guards as $guard) {
47 2
            if ($guard->condition()) {
48 1
                if (isset($this->exception))
49
                    throw $this->exception;
50
                else
51 1
                    throw $guard->exception();
52
            }
53
        }
54 2
    }
55
}
56