ClientBuilder::getPropertyValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Laravel GitLab.
7
 *
8
 * (c) Graham Campbell <[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 GrahamCampbell\GitLab\Http;
15
16
use Gitlab\HttpClient\Builder;
17
use Http\Client\Common\HttpMethodsClient;
18
use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator;
19
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
20
use Http\Client\Common\Plugin\CachePlugin;
21
use Http\Client\Common\PluginClientFactory;
22
use Psr\Cache\CacheItemPoolInterface;
23
use ReflectionClass;
24
25
/**
26
 * This is the client builder class.
27
 *
28
 * @author Graham Campbell <[email protected]>
29
 */
30
class ClientBuilder extends Builder
31
{
32
    /**
33
     * The default cache lifetime of 48 hours.
34
     *
35
     * @var int
36
     */
37
    const DEFAULT_CACHE_LIFETIME = 172800;
38
39
    /**
40
     * The cache plugin to use.
41
     *
42
     * This plugin is specially treated because it has to be the very last plugin.
43
     *
44
     * @var \Http\Client\Common\Plugin\CachePlugin|null
45
     */
46
    private $cachePlugin;
47
48
    /**
49
     * Get the gitlab http client.
50
     *
51
     * @return \Http\Client\Common\HttpMethodsClient
52
     */
53 84
    public function getHttpClient()
54
    {
55 84
        if ($this->getPropertyValue('httpClientModified')) {
56 84
            $this->setPropertyValue('httpClientModified', false);
57 84
            $this->setPropertyValue('pluginClient', new HttpMethodsClient(
58 84
                (new PluginClientFactory())->createClient($this->getPropertyValue('httpClient'), $this->getPlugins()),
59 84
                $this->getPropertyValue('requestFactory')
60
            ));
61
        }
62
63 84
        return $this->getPropertyValue('pluginClient');
64
    }
65
66
    /**
67
     * Get the plugins to use.
68
     *
69
     * @return \Http\Client\Common\Plugin[]
70
     */
71 84
    private function getPlugins()
72
    {
73 84
        $plugins = $this->getPropertyValue('plugins');
74
75 84
        if ($this->cachePlugin) {
76 24
            $plugins[] = $this->cachePlugin;
77
        }
78
79 84
        return $plugins;
80
    }
81
82
    /**
83
     * Add a cache plugin to cache responses locally.
84
     *
85
     * @param \Psr\Cache\CacheItemPoolInterface $cachePool
86
     * @param array                             $config
87
     *
88
     * @return void
89
     */
90 24
    public function addCache(CacheItemPoolInterface $cachePool, array $config = [])
91
    {
92 24
        $this->setCachePlugin(
93 24
            $cachePool,
94 24
            $config['generator'] ?? new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']),
95 24
            $config['lifetime'] ?? self::DEFAULT_CACHE_LIFETIME
96
        );
97
98 24
        $this->setPropertyValue('httpClientModified', true);
99 24
    }
100
101
    /**
102
     * Add a cache plugin to cache responses locally.
103
     *
104
     * @param \Psr\Cache\CacheItemPoolInterface                            $cachePool
105
     * @param \Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator $generator
106
     * @param int                                                          $lifetime
107
     *
108
     * @return void
109
     */
110 24
    private function setCachePlugin(CacheItemPoolInterface $cachePool, CacheKeyGenerator $generator, int $lifetime)
111
    {
112 24
        $stream = $this->getPropertyValue('streamFactory');
113
114 24
        $options = ['cache_lifetime' => $lifetime, 'cache_key_generator' => $generator];
115
116 24
        $this->cachePlugin = CachePlugin::clientCache($cachePool, $stream, $options);
117 24
    }
118
119
    /**
120
     * Get the value of the given private property on the builder.
121
     *
122
     * @param string $name
123
     *
124
     * @return mixed
125
     */
126 84
    private function getPropertyValue(string $name)
127
    {
128 84
        return self::getProperty($name)->getValue($this);
129
    }
130
131
    /**
132
     * Set the value of the given private property on the builder.
133
     *
134
     * @param string $name
135
     * @param mixed  $value
136
     *
137
     * @return void
138
     */
139 84
    private function setPropertyValue(string $name, $value)
140
    {
141 84
        self::getProperty($name)->setValue($this, $value);
142 84
    }
143
144
    /**
145
     * Get the builder reflection property for the given name.
146
     *
147
     * @param string $name
148
     *
149
     * @return \ReflectionProperty
150
     */
151 84
    private static function getProperty(string $name)
152
    {
153 84
        $prop = (new ReflectionClass(Builder::class))->getProperty($name);
154
155 84
        $prop->setAccessible(true);
156
157 84
        return $prop;
158
    }
159
}
160