Curl   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 12
c 4
b 1
f 1
lcom 2
cbo 0
dl 0
loc 140
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A cacheIndexExists() 0 15 2
A createCacheIndex() 0 8 1
A get() 0 17 4
A curlHandler() 0 9 1
A delete() 0 8 1
A set() 0 12 1
A drop() 0 7 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 4/6/15
5
 * Time: 6:30 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Cache\Adapter\ElasticSearch;
12
13
/**
14
 * Class Curl
15
 * @package NilPortugues\Cache\Adapter\ElasticSearch
16
 */
17
class Curl implements CurlClient
18
{
19
    /**
20
     * @var string
21
     */
22
    private $base;
23
24
    /**
25
     * @var string
26
     */
27
    private $baseUrl;
28
29
    /**
30
     * @param string $base
31
     * @param string $baseUrl
32
     */
33
    public function __construct($base, $baseUrl)
34
    {
35
        $this->base = $base;
36
        $this->baseUrl = $baseUrl;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    public function cacheIndexExists()
43
    {
44
        $curlHandler = \curl_init();
45
        \curl_setopt($curlHandler, CURLOPT_URL, \sprintf("{$this->base}/%s", '_settings'));
46
        \curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
47
48
        $response = \curl_exec($curlHandler);
49
50
        if (false !== $response) {
51
            $response = \json_decode($response, true);
52
            return \array_key_exists('index_name', $response);
53
        }
54
55
        return false;
56
    }
57
58
    /**
59
     * @param       $base
60
     * @param array $createCache
61
     * @return void
62
     */
63
    public function createCacheIndex($base, array $createCache)
64
    {
65
        $curlHandler = \curl_init();
66
        \curl_setopt($curlHandler, CURLOPT_URL, $base);
67
        \curl_setopt($curlHandler, CURLOPT_POST, true);
68
        \curl_setopt($curlHandler, CURLOPT_POSTFIELDS, \json_encode($createCache));
69
        \curl_exec($curlHandler);
70
    }
71
72
73
    /**
74
     * @param string $key
75
     *
76
     * @return mixed|null
77
     */
78
    public function get($key)
79
    {
80
        $curlHandler = $this->curlHandler($key.'?fields=_source,_ttl');
81
82
        $response = \curl_exec($curlHandler);
83
        \curl_close($curlHandler);
84
85
        if (false !== $response) {
86
            $response = \json_decode($response, true);
87
88
            if (true === $response['exists'] && $response['fields']['_ttl'] > 0) {
89
                return $response["_source"]['value'];
90
            }
91
        }
92
93
        return null;
94
    }
95
96
97
    /**
98
     * @param string $key
99
     *
100
     * @return resource
101
     */
102
    public function curlHandler($key)
103
    {
104
        $curlHandler = \curl_init();
105
106
        \curl_setopt($curlHandler, CURLOPT_URL, \sprintf("{$this->baseUrl}/%s", $key));
107
        \curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
108
109
        return $curlHandler;
110
    }
111
112
    /**
113
     * @param $key
114
     */
115
    public function delete($key)
116
    {
117
        $curlHandler = $this->curlHandler($key);
118
        \curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, "DELETE");
119
120
        \curl_exec($curlHandler);
121
        \curl_close($curlHandler);
122
    }
123
124
125
    /**
126
     * @param $key
127
     * @param $value
128
     * @param $ttl
129
     *
130
     * @return mixed
131
     */
132
    public function set($key, $value, $ttl)
133
    {
134
        $curlHandler = $this->curlHandler($key . '?ttl=' . $ttl . 's');
135
136
        \curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
137
        \curl_setopt($curlHandler, CURLOPT_POST, true);
138
        \curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $value);
139
140
        $response = \curl_exec($curlHandler);
141
        \curl_close($curlHandler);
142
        return $response;
143
    }
144
145
146
    /**
147
     *
148
     */
149
    public function drop($base)
150
    {
151
        $curlHandler = \curl_init();
152
        \curl_setopt($curlHandler, CURLOPT_URL, $base);
153
        \curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, "DELETE");
154
        \curl_exec($curlHandler);
155
    }
156
}
157