1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Endpoints\Indices;
6
7use Elasticsearch\Endpoints\AbstractEndpoint;
8use Elasticsearch\Common\Exceptions;
9
10/**
11 * Class Rollover
12 *
13 * @category Elasticsearch
14 * @package  Elasticsearch\Endpoints\Indices
15 * @author   Zachary Tong <zach@elastic.co>
16 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache2
17 * @link     http://elastic.co
18 */
19class Rollover extends AbstractEndpoint
20{
21    private $alias;
22    private $newIndex;
23
24    public function setAlias(?string $alias): Rollover
25    {
26        if ($alias === null) {
27            return $this;
28        }
29
30        $this->alias = urlencode($alias);
31        return $this;
32    }
33
34    public function setNewIndex(?string $newIndex): Rollover
35    {
36        if ($newIndex === null) {
37            return $this;
38        }
39
40        $this->newIndex = urlencode($newIndex);
41        return $this;
42    }
43
44    public function setBody($body): Rollover
45    {
46        if (isset($body) !== true) {
47            return $this;
48        }
49
50        $this->body = $body;
51
52        return $this;
53    }
54
55    public function getURI(): string
56    {
57        if (isset($this->alias) !== true) {
58            throw new Exceptions\RuntimeException(
59                'alias name is required for Rollover'
60            );
61        }
62        $alias = $this->alias;
63        $newIndex = $this->newIndex ?? null;
64        if (isset($newIndex)) {
65            return "/$alias/_rollover/$newIndex";
66        }
67        return "/$alias/_rollover";
68    }
69
70    public function getParamWhitelist(): array
71    {
72        return [
73            'include_type_name',
74            'timeout',
75            'dry_run',
76            'master_timeout',
77            'wait_for_active_shards'
78        ];
79    }
80
81    public function getMethod(): string
82    {
83        return 'POST';
84    }
85}
86