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

Guard::resolveException()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
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