HasSyntaxCorrectCheck::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Check;
4
5
use Symfony\Component\Process\Exception\ProcessTimedOutException;
6
use Tworzenieweb\SqlProvisioner\Database\Parser;
7
use Tworzenieweb\SqlProvisioner\Model\Candidate;
8
9
/**
10
 * @author Luke Adamczewski
11
 * @package Tworzenieweb\SqlProvisioner\Check
12
 */
13
class HasSyntaxCorrectCheck implements CheckInterface
14
{
15
    const ERROR_STATUS = 'HAS_SYNTAX_ERROR';
16
17
    /** @var Parser */
18
    private $parser;
19
20
    /** @var string */
21
    private $lastError;
22
23
24
25
    /**
26
     * @param Parser $parser
27
     */
28 1
    public function __construct(Parser $parser)
29
    {
30 1
        $this->parser = $parser;
31 1
    }
32
33
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function execute(Candidate $candidate): bool
39
    {
40
        $this->lastError = null;
41
42
        try {
43
            $parsingResult = $this->parser->execute($candidate);
44
45
            if (!empty($parsingResult)) {
46
                $this->lastError = sprintf("Syntax error during processing of %s:\n%s", $candidate->getName(), $parsingResult);
47
48
                return true;
49
            }
50
        } catch (ProcessTimedOutException $processTimedOutException) {
51
            $this->lastError = $processTimedOutException->getMessage() . "\nHint: you can disable syntax checking for queries too big for processing";
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getLastErrorMessage(): string
61
    {
62
        return $this->lastError;
63
    }
64
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function getErrorCode(): string
70
    {
71
        return self::ERROR_STATUS;
72
    }
73
}
74