TemplateQuery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 123
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFile() 0 4 1
A setFile() 0 6 1
A getInline() 0 4 1
A setInline() 0 6 1
A getParams() 0 4 1
A setParams() 0 6 1
A getType() 0 4 1
A toArray() 0 20 3
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchDSL\Query\Specialized;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Represents Elasticsearch "template" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html
21
 */
22
class TemplateQuery implements BuilderInterface
23
{
24
    use ParametersTrait;
25
26
    /**
27
     * @var string
28
     */
29
    private $file;
30
31
    /**
32
     * @var string
33
     */
34
    private $inline;
35
36
    /**
37
     * @var array
38
     */
39
    private $params;
40
41
    /**
42
     * @param string $file A template of the query
43
     * @param string $inline A template of the query
44
     * @param array  $params Parameters to insert into template
45
     */
46
    public function __construct($file = null, $inline = null, array $params = [])
47
    {
48
        $this->setFile($file);
49
        $this->setInline($inline);
50
        $this->setParams($params);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getFile()
57
    {
58
        return $this->file;
59
    }
60
61
    /**
62
     * @param string $file
63
     *
64
     * @return $this;
0 ignored issues
show
Documentation introduced by
The doc-type $this; could not be parsed: Expected "|" or "end of type", but got ";" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
65
     */
66
    public function setFile($file)
67
    {
68
        $this->file = $file;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getInline()
77
    {
78
        return $this->inline;
79
    }
80
81
    /**
82
     * @param string $inline
83
     *
84
     * @return $this
85
     */
86
    public function setInline($inline)
87
    {
88
        $this->inline = $inline;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getParams()
97
    {
98
        return $this->params;
99
    }
100
101
    /**
102
     * @param array $params
103
     *
104
     * @return $this
105
     */
106
    public function setParams($params)
107
    {
108
        $this->params = $params;
109
110
        return $this;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getType()
117
    {
118
        return 'template';
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function toArray()
125
    {
126
        $output = array_filter(
127
            [
128
                'file' => $this->getFile(),
129
                'inline' => $this->getInline(),
130
                'params' => $this->getParams(),
131
            ]
132
        );
133
134
        if (!isset($output['file']) && !isset($output['inline'])) {
135
            throw new \InvalidArgumentException(
136
                'Template query requires that either `inline` or `file` parameters are set'
137
            );
138
        }
139
140
        $output = $this->processArray($output);
141
142
        return [$this->getType() => $output];
143
    }
144
}
145