ClientFactory::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.9
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the SeamsCMSDeliverySdk package.
7
 *
8
 * (c) Seams-CMS.com
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 SeamsCMS\Delivery;
15
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
/**
19
 * Class ClientFactory
20
 * @package SeamsCMS\Delivery
21
 */
22
class ClientFactory
23
{
24
    /**
25
     * @param string $apiKey
26
     * @param string $workspace
27
     * @param array $options
28
     * @return Client
29
     */
30 1
    public function build(string $apiKey, string $workspace, $options = array()): Client
31
    {
32 1
        $resolver = new OptionsResolver();
33 1
        $this->configureOptions($resolver);
34 1
        $resolvedOptions = $resolver->resolve($options);
35
36
        // Merge guzzle options and override with our own
37 1
        $options = $resolvedOptions['guzzle_options'];
38 1
        $options = array_merge($options, [
39 1
            'base_uri' => $resolvedOptions['host'],
40 1
            'debug' => $resolvedOptions['debug'],
41
            'headers' => [
42 1
                'Authorization' => "Bearer " . $apiKey,
43
            ],
44
        ]);
45
46 1
        $guzzleClient = new \GuzzleHttp\Client($options);
47
48 1
        return new Client($guzzleClient, $workspace);
49
    }
50
51
    /**
52
     * @param OptionsResolver $resolver
53
     * @return void
54
     */
55 1
    public function configureOptions(OptionsResolver $resolver)
56
    {
57 1
        $resolver->setDefaults([
58 1
            'host' => 'https://delivery.seams-api.com/',
59
            'debug' => false,
60
            'guzzle_options' => [],
61
        ]);
62 1
    }
63
}
64