Completed
Push — master ( e5a5c3...c616da )
by Dominik
03:36 queued 01:07
created

QueryBuilder::json()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.0416
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 16
    public function __construct()
22
    {
23 16
        $this->rootNode = new ObjectNode();
24 16
        $this->node = $this->rootNode;
25 16
    }
26
27
    /**
28
     * @param AbstractNode $node
29
     * @param bool         $allowDefault
30
     *
31
     * @return $this
32
     *
33
     * @throws \Exception
34
     */
35 3 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...
36
    {
37 3
        if (!$this->node instanceof ArrayNode) {
38 1
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
39
        }
40
41 2
        $this->node->add($node, $allowDefault);
42 2
        $this->reassignParent($node);
43
44 2
        return $this;
45
    }
46
47
    /**
48
     * @param string       $key
49
     * @param AbstractNode $node
50
     * @param bool         $allowDefault
51
     *
52
     * @return $this
53
     *
54
     * @throws \Exception
55
     */
56 15 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...
57
    {
58 15
        if (!$this->node instanceof ObjectNode) {
59 1
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
60
        }
61
62 15
        $this->node->add($key, $node, $allowDefault);
63 15
        $this->reassignParent($node);
64
65 15
        return $this;
66
    }
67
68
    /**
69
     * @param AbstractNode $node
70
     */
71 15
    protected function reassignParent(AbstractNode $node)
72
    {
73 15
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
74 15
            $this->node = $node;
75 15
        }
76 15
    }
77
78
    /**
79
     * @return $this
80
     */
81 1
    public function end()
82
    {
83 1
        $this->node = $this->node->getParent();
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * @return \stdClass|null
90
     */
91 14
    public function serialize()
92
    {
93 14
        return $this->rootNode->serialize();
94
    }
95
96
    /**
97
     * @param boolean $beautify
98
     * @return string
99
     */
100 14
    public function json($beautify = false)
101
    {
102 14
        if (null === $serialized = $this->serialize()) {
103
            return '';
104
        }
105
106 14
        if ($beautify) {
107 1
            return json_encode($serialized, JSON_PRETTY_PRINT);
108
        }
109
110 13
        return json_encode($serialized);
111
    }
112
}
113