This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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 NilPortugues\Cache\Adapter; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use NilPortugues\Cache\Adapter\ElasticSearch\Curl; |
||
7 | use NilPortugues\Cache\CacheAdapter; |
||
8 | |||
9 | /** |
||
10 | * Class ElasticSearchAdapter |
||
11 | * @package NilPortugues\Cache\Adapter |
||
12 | */ |
||
13 | class ElasticSearchAdapter extends Adapter implements CacheAdapter |
||
14 | { |
||
15 | /** |
||
16 | * @var \NilPortugues\Cache\Adapter\ElasticSearch\Curl |
||
17 | */ |
||
18 | private $curl; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $base = ''; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $baseUrl = ''; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $createCache = [ |
||
34 | "mappings" => [ |
||
35 | "cache" => [ |
||
36 | "_source" => [ |
||
37 | "enabled" => false |
||
38 | ], |
||
39 | "_ttl" => [ |
||
40 | "enabled" => true, |
||
41 | "default" => "1000ms" |
||
42 | ], |
||
43 | "properties" => [ |
||
44 | "value" => [ |
||
45 | "type" => "string", |
||
46 | "index" => "not_analyzed" |
||
47 | ] |
||
48 | ] |
||
49 | ] |
||
50 | ] |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @param string $baseUrl |
||
55 | * @param string $indexName |
||
56 | * @param CacheAdapter $next |
||
57 | * |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | public function __construct($baseUrl, $indexName, CacheAdapter $next = null) |
||
61 | { |
||
62 | $baseUrl = (string)$baseUrl; |
||
63 | $indexName = (string)$indexName; |
||
64 | $this->curl = $this->getCurlClient(); |
||
65 | |||
66 | $this->base = \sprintf("%s/%s", $baseUrl, $indexName); |
||
67 | $this->baseUrl = \sprintf("%s/%s/cache", $baseUrl, $indexName); |
||
68 | |||
69 | if (false === \filter_var($this->base, FILTER_VALIDATE_URL)) { |
||
70 | throw new InvalidArgumentException('The provided base URL is not a valid URL'); |
||
71 | } |
||
72 | |||
73 | if (false === $this->curl->cacheIndexExists()) { |
||
74 | $this->curl->createCacheIndex($this->base, $this->createCache); |
||
75 | } |
||
76 | |||
77 | $this->nextAdapter = (InMemoryAdapter::getInstance() === $next) ? null : $next; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @codeCoverageIgnore |
||
82 | * @return Curl |
||
83 | */ |
||
84 | protected function getCurlClient() |
||
85 | { |
||
86 | return new Curl($this->base, $this->baseUrl); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get a value identified by $key. |
||
91 | * |
||
92 | * @param string $key |
||
93 | * |
||
94 | * @return bool|mixed |
||
95 | */ |
||
96 | View Code Duplication | public function get($key) |
|
0 ignored issues
–
show
|
|||
97 | { |
||
98 | $key = (string)$key; |
||
99 | $this->hit = false; |
||
100 | |||
101 | $inMemoryValue = InMemoryAdapter::getInstance()->get($key); |
||
102 | if (InMemoryAdapter::getInstance()->isHit()) { |
||
103 | $this->hit = true; |
||
104 | return $inMemoryValue; |
||
105 | } |
||
106 | |||
107 | $value = $this->curl->get($key); |
||
108 | |||
109 | if (null !== $value) { |
||
110 | $this->hit = true; |
||
111 | InMemoryAdapter::getInstance()->set($key, $value, 0); |
||
112 | return $this->restoreDataStructure($value); |
||
113 | } |
||
114 | |||
115 | return (null !== $this->nextAdapter) ? $this->nextAdapter->get($key) : null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Delete a value identified by $key. |
||
120 | * |
||
121 | * @param string $key |
||
122 | */ |
||
123 | public function delete($key) |
||
124 | { |
||
125 | $this->curl->delete($key); |
||
126 | $this->deleteChain($key); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Set a value identified by $key and with an optional $ttl. |
||
131 | * |
||
132 | * @param string $key |
||
133 | * @param mixed $value |
||
134 | * @param int $ttl |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function set($key, $value, $ttl = 0) |
||
139 | { |
||
140 | $ttl = $this->fromDefaultTtl($ttl); |
||
141 | |||
142 | if ($ttl >= 0) { |
||
143 | $response = $this->curl->set($key, $this->storageDataStructure($value), $ttl); |
||
144 | |||
145 | if (false !== $response) { |
||
146 | $response = \json_decode($response, true); |
||
147 | |||
148 | if (\array_key_exists('ok', $response) && true === $response['ok']) { |
||
149 | $this->setChain($key, $value, $ttl); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Checks the availability of the cache service. |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function isAvailable() |
||
163 | { |
||
164 | return $this->curl->cacheIndexExists(); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Clears all expired values from cache. |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | public function clear() |
||
173 | { |
||
174 | $this->clearChain(); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Clears all values from the cache. |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function drop() |
||
183 | { |
||
184 | $this->curl->drop($this->base); |
||
185 | $this->curl->createCacheIndex($this->base, $this->createCache); |
||
186 | $this->dropChain(); |
||
187 | } |
||
188 | } |
||
189 |
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.