Completed
Push — master ( b29a51...5a6932 )
by Łukasz
08:39 queued 04:56
created

CandidateProcessor::addPostCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 1
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
    public function __construct()
34
    {
35
        $this->postChecks = [];
36
        $this->checks = [];
37
    }
38
39
40
41
    /**
42
     * @param CheckInterface $check
43
     */
44
    public function addCheck(CheckInterface $check)
45
    {
46
        array_push($this->checks, $check);
47
    }
48
49
50
51
    public function addPostCheck(CheckInterface $check)
52
    {
53
        array_push($this->postChecks, $check);
54
    }
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
}