Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
created

lib/Elastica/IndexTemplate.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica;
4
5
use Elastica\Exception\InvalidException;
6
7
/**
8
 * Elastica index template object.
9
 *
10
 * @author Dmitry Balabka <[email protected]>
11
 *
12
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
13
 */
14
class IndexTemplate
15
{
16
    /**
17
     * Index template name.
18
     *
19
     * @var string Index pattern
20
     */
21
    protected $_name;
22
23
    /**
24
     * Client object.
25
     *
26
     * @var \Elastica\Client Client object
27
     */
28
    protected $_client;
29
30
    /**
31
     * Creates a new index template object.
32
     *
33
     * @param \Elastica\Client $client Client object
34
     * @param string           $name   Index template name
35
     *
36
     * @throws \Elastica\Exception\InvalidException
37
     */
38 View Code Duplication
    public function __construct(Client $client, $name)
0 ignored issues
show
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...
39
    {
40
        $this->_client = $client;
41
42
        if (!is_scalar($name)) {
43
            throw new InvalidException('Index template should be a scalar type');
44
        }
45
        $this->_name = (string) $name;
46
    }
47
48
    /**
49
     * Deletes the index template.
50
     *
51
     * @return \Elastica\Response Response object
52
     */
53
    public function delete()
54
    {
55
        $response = $this->request(Request::DELETE);
56
57
        return $response;
58
    }
59
60
    /**
61
     * Creates a new index template with the given arguments.
62
     *
63
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
64
     *
65
     * @param array $args OPTIONAL Arguments to use
66
     *
67
     * @return \Elastica\Response
68
     */
69
    public function create(array $args = [])
70
    {
71
        return $this->request(Request::PUT, $args);
72
    }
73
74
    /**
75
     * Checks if the given index template is already created.
76
     *
77
     * @return bool True if index exists
78
     */
79
    public function exists()
80
    {
81
        $response = $this->request(Request::HEAD);
82
83
        return 200 === $response->getStatus();
84
    }
85
86
    /**
87
     * Returns the index template name.
88
     *
89
     * @return string Index name
90
     */
91
    public function getName()
92
    {
93
        return $this->_name;
94
    }
95
96
    /**
97
     * Returns index template client.
98
     *
99
     * @return \Elastica\Client Index client object
100
     */
101
    public function getClient()
102
    {
103
        return $this->_client;
104
    }
105
106
    /**
107
     * Makes calls to the elasticsearch server based on this index template name.
108
     *
109
     * @param string $method Rest method to use (GET, POST, DELETE, PUT)
110
     * @param array  $data   OPTIONAL Arguments as array
111
     *
112
     * @return \Elastica\Response Response object
113
     */
114
    public function request($method, $data = [])
115
    {
116
        $path = '_template/'.$this->getName();
117
118
        return $this->getClient()->request($path, $method, $data);
119
    }
120
}
121