AbstractChecker   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 60
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 0 15 5
A tryMethod() 0 17 3
1
<?php
2
3
namespace Simplario\Checker\Checker;
4
5
use Simplario\Checker\ResultException\ErrorException;
6
use Simplario\Checker\ResultException\FailException;
7
use Simplario\Checker\ResultException\SuccessException;
8
9
/**
10
 * Class AbstractChecker
11
 *
12
 * @package Simplario\Checker\Checker
13
 */
14 23
abstract class AbstractChecker
15
{
16 23
    /**
17
     * @var array
18 23
     */
19 23
    protected $ignoreTaskKeys = [];
20
21
    /**
22 23
     * @var string
23 23
     */
24
    protected $target = 'target';
25
26 22
    /**
27 22
     * @param array $task
28
     *
29
     * @throws ErrorException
30
     * @throws SuccessException
31 11
     */
32
    public function check(array $task)
33
    {
34 3
        foreach ($task as $test => $options) {
35
36
            if ($test === 'checker' || $test === $this->target || in_array($test, $this->ignoreTaskKeys, true)) {
37
                continue;
38 11
            }
39
40
            $method = 'test' . ucfirst($test);
41
            $this->tryMethod($method, $task, $options);
42
        }
43
44
        // final success
45
        throw new SuccessException("Ok", $task);
46
    }
47
48
    /**
49
     * @param string $method
50
     * @param array  $task
51
     * @param mixed  $options
52
     *
53
     * @return $this
54
     * @throws ErrorException
55
     */
56
    protected function tryMethod($method, array $task, $options)
57
    {
58
        if (!method_exists($this, $method)) {
59
            throw new ErrorException("Test method not exists '{$method}'", $task);
60
        }
61
62
        try {
63
            // if test success - throw SuccessException
64
            // if test fail - throw FailException
65
            // if test error - ErrorException
66
            call_user_func_array([$this, $method], [$task[$this->target], $options, $task]);
67
        } catch (SuccessException $ex) {
68
            // - test success
69
        }
70
71
        return $this;
72
    }
73
}
74