Completed
Push — master ( ae3e1f...594fe9 )
by Dominik
03:33
created

QueryBuilder::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
3
namespace Saxulum\ElasticSearchQueryBuilder;
4
5
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
6
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
7
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
8
9
class QueryBuilder
10
{
11
    /**
12
     * @var ObjectNode
13
     */
14
    protected $rootNode;
15
16
    /**
17
     * @var AbstractNode
18
     */
19
    protected $node;
20
21 14
    public function __construct()
22
    {
23 14
        $this->rootNode = new ObjectNode();
24 14
        $this->node = $this->rootNode;
25 14
    }
26
27
    /**
28
     * @param AbstractNode $node
29
     * @param bool $allowDefault
30
     * @return $this
31
     * @throws \Exception
32
     */
33 2 View Code Duplication
    public function addToArrayNode(AbstractNode $node, $allowDefault = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35 2
        if (!$this->node instanceof ArrayNode) {
36
            throw new \Exception(sprintf('You cannot call %s on node type %s', __METHOD__, get_class($this->node)));
37
        }
38
        
39 2
        $this->node->add($node, $allowDefault);
40 2
        $this->reassignParent($node);
41
        
42 2
        return $this;
43
    }
44
45
    /**
46
     * @param string $key
47
     * @param AbstractNode $node
48
     * @param bool $allowDefault
49
     * @return $this
50
     * @throws \Exception
51
     */
52 14 View Code Duplication
    public function addToObjectNode($key, AbstractNode $node, $allowDefault = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 14
        if (!$this->node instanceof ObjectNode) {
55
            throw new \Exception(sprintf('You cannot call %s on node type %s', __METHOD__, get_class($this->node)));
56
        }
57
58 14
        $this->node->add($key, $node, $allowDefault);
59 14
        $this->reassignParent($node);
60
        
61 14
        return $this;
62
    }
63
64
    /**
65
     * @param AbstractNode $node
66
     */
67 14
    protected function reassignParent(AbstractNode $node)
68
    {
69 14
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
70 14
            $this->node = $node;
71 14
        }
72 14
    }
73
74
    /**
75
     * @return $this
76
     */
77 1
    public function end()
78
    {
79 1
        $this->node = $this->node->getParent();
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * @return Query
86
     */
87 14
    public function query()
88
    {
89 14
        return new Query($this->rootNode);
90
    }
91
}
92