HasSyntaxCorrectCheck   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 18.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 61
ccs 3
cts 16
cp 0.1875
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 18 3
A getLastErrorMessage() 0 4 1
A getErrorCode() 0 4 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