1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Laravel Paystack package. |
7
|
|
|
* |
8
|
|
|
* (c) Prosper Otemuyiwa <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Unicodeveloper\Paystack\Http; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
use GrahamCampbell\CachePlugin\CachePlugin; |
18
|
|
|
use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator; |
19
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
20
|
|
|
use Xeviant\Paystack\HttpClient\Builder; |
21
|
|
|
|
22
|
|
|
class ClientBuilder extends Builder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Adds Cache Plugin to builder |
26
|
|
|
* |
27
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
28
|
|
|
* @param array $config |
29
|
|
|
* @throws \ReflectionException |
30
|
|
|
*/ |
31
|
|
|
public function addCache(CacheItemPoolInterface $cacheItemPool, array $config = []) |
32
|
|
|
{ |
33
|
|
|
$this->setCachePlugin($cacheItemPool, $config['generator'] ?? null, $config['lifetime'] ?? null); |
34
|
|
|
|
35
|
|
|
$this->setPropertyValue('httpClientModified', true); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a cache plugin to cache responses locally. |
40
|
|
|
* |
41
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
42
|
|
|
* @param CacheKeyGenerator|null $generator |
43
|
|
|
* @param int|null $lifetime |
44
|
|
|
* @throws \ReflectionException |
45
|
|
|
*/ |
46
|
|
|
protected function setCachePlugin(CacheItemPoolInterface $cacheItemPool, CacheKeyGenerator $generator = null, int $lifetime = null): void |
47
|
|
|
{ |
48
|
|
|
$stream = $this->getPropertyValue('streamFactory'); |
49
|
|
|
|
50
|
|
|
$this->setPropertyValue('cachePlugin', new CachePlugin($cacheItemPool, $stream, $generator, $lifetime)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieves the value of a builder property |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* @return mixed |
58
|
|
|
* @throws \ReflectionException |
59
|
|
|
*/ |
60
|
|
|
protected function getPropertyValue(string $name) |
61
|
|
|
{ |
62
|
|
|
return static::getProperty($name)->getValue($this); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets the value of a builder property |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @param $value |
70
|
|
|
* @throws \ReflectionException |
71
|
|
|
*/ |
72
|
|
|
protected function setPropertyValue(string $name, $value) |
73
|
|
|
{ |
74
|
|
|
return static::getProperty($name)->setValue($this, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets the builder reflection property for the given name |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @return \ReflectionProperty |
82
|
|
|
* @throws \ReflectionException |
83
|
|
|
*/ |
84
|
|
|
protected static function getProperty(string $name) |
85
|
|
|
{ |
86
|
|
|
$prop = (new \ReflectionClass(Builder::class))->getProperty($name); |
87
|
|
|
|
88
|
|
|
$prop->setAccessible(true); |
89
|
|
|
|
90
|
|
|
return $prop; |
91
|
|
|
} |
92
|
|
|
} |