GitLabFactory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 108
ccs 24
cts 24
cp 1
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheAdapter() 0 7 3
A __construct() 0 4 1
A getBuilder() 0 13 4
A make() 0 17 4
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;
15
16
use Gitlab\Client;
17
use GrahamCampbell\GitLab\Authenticators\AuthenticatorFactory;
18
use GrahamCampbell\GitLab\Http\ClientBuilder;
19
use GrahamCampbell\GitLab\Http\Psr16Cache;
20
use Http\Client\Common\Plugin\RetryPlugin;
21
use Illuminate\Contracts\Cache\Factory;
22
use Illuminate\Support\Arr;
23
use InvalidArgumentException;
24
use Symfony\Component\Cache\Adapter\Psr16Adapter;
25
use Symfony\Component\Cache\Adapter\SimpleCacheAdapter;
26
27
/**
28
 * This is the gitlab factory class.
29
 *
30
 * @author Graham Campbell <[email protected]>
31
 */
32
class GitLabFactory
33
{
34
    /**
35
     * The minimum cache lifetime of 12 hours.
36
     *
37
     * @var int
38
     */
39
    const MIN_CACHE_LIFETIME = 43200;
40
41
    /**
42
     * The maximum cache lifetime of 48 hours.
43
     *
44
     * @var int
45
     */
46
    const MAX_CACHE_LIFETIME = 172800;
47
48
    /**
49
     * The authenticator factory instance.
50
     *
51
     * @var \GrahamCampbell\GitLab\Authenticators\AuthenticatorFactory
52
     */
53
    protected $auth;
54
55
    /**
56
     * The illuminate cache factory instance.
57
     *
58
     * @var \Illuminate\Contracts\Cache\Factory
59
     */
60
    protected $cache;
61
62
    /**
63
     * Create a new gitlab factory instance.
64
     *
65
     * @param \GrahamCampbell\GitLab\Authenticators\AuthenticatorFactory $auth
66
     * @param \Illuminate\Contracts\Cache\Factory                        $cache
67
     *
68
     * @return void
69
     */
70 156
    public function __construct(AuthenticatorFactory $auth, Factory $cache)
71
    {
72 156
        $this->auth = $auth;
73 156
        $this->cache = $cache;
74 156
    }
75
76
    /**
77
     * Make a new gitlab client.
78
     *
79
     * @param string[] $config
80
     *
81
     * @throws \InvalidArgumentException
82
     *
83
     * @return \Gitlab\Client
84
     */
85 120
    public function make(array $config)
86
    {
87 120
        $client = new Client($this->getBuilder($config));
88
89 120
        if (!array_key_exists('method', $config)) {
90 12
            throw new InvalidArgumentException('The gitlab factory requires an auth method.');
91
        }
92
93 108
        if ($url = Arr::get($config, 'url')) {
94 12
            $client->setUrl($url);
95
        }
96
97 108
        if ($config['method'] === 'none') {
98 12
            return $client;
99
        }
100
101 96
        return $this->auth->make($config['method'])->with($client)->authenticate($config);
102
    }
103
104
    /**
105
     * Get the http client builder.
106
     *
107
     * @param string[] $config
108
     *
109
     * @return \GrahamCampbell\GitLab\Http\ClientBuilder
110
     */
111 120
    protected function getBuilder(array $config)
112
    {
113 120
        $builder = new ClientBuilder();
114
115 120
        if ($backoff = Arr::get($config, 'backoff')) {
116 12
            $builder->addPlugin(new RetryPlugin(['retries' => $backoff === true ? 2 : $backoff]));
117
        }
118
119 120
        if ($cache = Arr::get($config, 'cache')) {
120 24
            $builder->addCache($this->getCacheAdapter($cache));
121
        }
122
123 120
        return $builder;
124
    }
125
126
    /**
127
     * Get the symfony cache adapter for the given illuminate store.
128
     *
129
     * @param bool|string $name
130
     *
131
     * @return \Symfony\Component\Cache\Adapter\AdapterInterface
132
     */
133 24
    protected function getCacheAdapter($name)
134
    {
135 24
        $store = $this->cache->store($name === true ? null : $name);
136
137 24
        $repo = new Psr16Cache($store, self::MIN_CACHE_LIFETIME, self::MAX_CACHE_LIFETIME);
138
139 24
        return class_exists(Psr16Adapter::class) ? new Psr16Adapter($repo) : new SimpleCacheAdapter($repo);
140
    }
141
}
142