Completed
Push — master ( 1dfc8b...437320 )
by Arthur
02:02
created

Guard   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 9
dl 0
loc 34
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveException() 0 11 4
A exception() 0 3 1
1
<?php
2
3
namespace Larapie\Guard;
4
5
use Larapie\Guard\Contracts\GuardContract;
6
use Larapie\Guard\Exceptions\ResolveFailedException;
7
8
/**
9
 * Class Guard.
10
 */
11
abstract class Guard implements GuardContract
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $exception;
17
18
    /**
19
     * The exception that gets thrown when the condition is satisfied.
20
     *
21
     * @return \Throwable
22
     * @throws ResolveFailedException
23
     */
24 2
    public function exception(): \Throwable
25
    {
26 2
        return $this->resolveException($this->exception);
27
    }
28
29
    /**
30
     * @param $exception
31
     * @return \Throwable
32
     * @throws ResolveFailedException
33
     */
34 2
    protected function resolveException($exception) :\Throwable
35
    {
36 2
        if (is_string($exception) || ! ($exception instanceof \Throwable)) {
37
            try {
38 2
                $exception = new $exception;
39 1
            } catch (\ArgumentCountError $e) {
40 1
                throw new ResolveFailedException();
41
            }
42
        }
43
44 1
        return $exception;
45
    }
46
}
47