CandidateProcessor::isValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Processor;
4
5
use RuntimeException;
6
use Tworzenieweb\SqlProvisioner\Check\CheckInterface;
7
use Tworzenieweb\SqlProvisioner\Model\Candidate;
8
9
/**
10
 * @author Luke Adamczewski
11
 * @package Tworzenieweb\SqlProvisioner\Processor
12
 */
13
class CandidateProcessor
14
{
15
    const FATAL_POST_CHECK_ERROR = 'Your candidate query has failed. There was no entry in target changelog table added. Try adding query manually for more error.';
16
    /** @var CheckInterface[] */
17
    private $checks;
18
19
    /** @var string */
20
    private $lastError;
21
22
    /** @var string */
23
    private $lastErrorMessage;
24
25
    /** @var CheckInterface[] */
26
    private $postChecks;
27
28
29
30
    /**
31
     * CandidateProcessor constructor
32
     */
33 1
    public function __construct()
34
    {
35 1
        $this->postChecks = [];
36 1
        $this->checks = [];
37 1
    }
38
39
40
41
    /**
42
     * @param CheckInterface $check
43
     */
44 1
    public function addCheck(CheckInterface $check)
45
    {
46 1
        array_push($this->checks, $check);
47 1
    }
48
49
50
51 1
    public function addPostCheck(CheckInterface $check)
52
    {
53 1
        array_push($this->postChecks, $check);
54 1
    }
55
56
    /**
57
     * @param Candidate $candidate
58
     * @return bool
59
     */
60
    public function isValid(Candidate $candidate)
61
    {
62
        $this->lastError = null;
63
        foreach ($this->checks as $check) {
64
            if ($check->execute($candidate)) {
65
                $this->lastError = $check->getErrorCode();
66
                $this->lastErrorMessage = $check->getLastErrorMessage();
67
68
                return false;
69
            }
70
        }
71
72
        return true;
73
    }
74
75
76
77
    /**
78
     * @param Candidate $candidate
79
     */
80
    public function postValidate(Candidate $candidate)
81
    {
82
        foreach ($this->postChecks as $check) {
83
            if (!$check->execute($candidate)) {
84
                throw new RuntimeException(sprintf(self::FATAL_POST_CHECK_ERROR));
85
            }
86
        }
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getLastError()
93
    {
94
        return $this->lastError;
95
    }
96
97
98
99
    /**
100
     * @return string
101
     */
102
    public function getLastErrorMessage()
103
    {
104
        return $this->lastErrorMessage;
105
    }
106
107
108
109
    /**
110
     * @param CheckInterface $check
111
     */
112
    public function removeCheck(CheckInterface $check)
113
    {
114
        $this->checks = array_filter($this->checks, function($currentCheck) use ($check) {
115
            return $check !== $currentCheck;
116
        });
117
    }
118
}
119