Completed
Push — master ( fa4f9a...8a9929 )
by Beniamin
03:39
created

QueryCompiler::addCompiler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\QueryBuilder\QueryCompiler;
13
14
use Phuria\QueryBuilder\QueryBuilder;
15
16
/**
17
 * @author Beniamin Jonatan Šimko <[email protected]>
18
 */
19
class QueryCompiler implements QueryCompilerInterface
20
{
21
    /**
22
     * @var \SplPriorityQueue|QueryCompilerInterface[] $compilers
23
     */
24
    private $compilers = [];
25
26 27
    public function __construct()
27
    {
28 27
        $this->compilers = new \SplPriorityQueue();
29
30 27
        $this->addCompiler(new SelectQueryCompiler());
31 27
        $this->addCompiler(new UpdateQueryCompiler());
32 27
    }
33
34
    /**
35
     * @param QueryCompilerInterface $compiler
36
     * @param int                    $priority
37
     *
38
     * @return $this
39
     */
40 27
    public function addCompiler(QueryCompilerInterface $compiler, $priority = 0)
41
    {
42 27
        $this->compilers->insert($compiler, $priority);
43
44 27
        return $this;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function canHandleQuery(QueryBuilder $qb)
51
    {
52
        return true;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 24
    public function compile(QueryBuilder $qb)
59
    {
60 24
        foreach ($this->compilers as $compiler) {
61 24
            if ($compiler->canHandleQuery($qb)) {
62 24
                return $compiler->compile($qb);
63
            }
64 2
        }
65
66
        return null;
67
    }
68
}