Completed
Pull Request — master (#71)
by Olatunbosun
16:04
created

ClientBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 71
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addCache() 0 6 1
A setCachePlugin() 0 6 1
A getPropertyValue() 0 4 1
A setPropertyValue() 0 4 1
A getProperty() 0 8 1
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
}