1<?php
2/**
3 * Elasticsearch PHP client
4 *
5 * @link      https://github.com/elastic/elasticsearch-php/
6 * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8 * @license   https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9 *
10 * Licensed to Elasticsearch B.V under one or more agreements.
11 * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12 * the GNU Lesser General Public License, Version 2.1, at your option.
13 * See the LICENSE file in the project root for more information.
14 */
15
16
17declare(strict_types = 1);
18
19namespace Elasticsearch\Namespaces;
20
21use Elasticsearch\Common\Exceptions\Missing404Exception;
22use Elasticsearch\Common\Exceptions\RoutingMissingException;
23use Elasticsearch\Endpoints\AbstractEndpoint;
24use Elasticsearch\Transport;
25use GuzzleHttp\Ring\Future\FutureArrayInterface;
26
27trait BooleanRequestWrapper
28{
29    /**
30     * Perform Request
31     *
32     * @throws Missing404Exception
33     * @throws RoutingMissingException
34     */
35    public static function performRequest(AbstractEndpoint $endpoint, Transport $transport)
36    {
37        try {
38            $response = $transport->performRequest(
39                $endpoint->getMethod(),
40                $endpoint->getURI(),
41                $endpoint->getParams(),
42                $endpoint->getBody(),
43                $endpoint->getOptions()
44            );
45
46            $response = $transport->resultOrFuture($response, $endpoint->getOptions());
47            if (!($response instanceof FutureArrayInterface)) {
48                if ($response['status'] === 200) {
49                    return true;
50                } else {
51                    return false;
52                }
53            } else {
54                // async mode, can't easily resolve this...punt to user
55                return $response;
56            }
57        } catch (Missing404Exception $exception) {
58            return false;
59        } catch (RoutingMissingException $exception) {
60            return false;
61        }
62    }
63}
64