Completed
Push — master ( 50f6e4...48039e )
by Hidde
09:27 queued 03:53
created

TransIPFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A getConfig() 0 8 2
A getClient() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of Laravel TransIP.
5
 *
6
 * (c) Hidde Beydals <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace TransIP\Laravel;
13
14
use TransIP\Client;
15
16
/**
17
 * @author Hidde Beydals <[email protected]>
18
 */
19
class TransIPFactory
20
{
21
    /**
22
     * Make a new TransIP client.
23
     *
24
     * @param array $config
25
     *
26
     * @return \HiddeCo\TransIP\Client
27
     */
28
    public function create(array $config)
29
    {
30
        $config = $this->getConfig($config);
31
32
        return $this->getClient($config);
33
    }
34
35
    /**
36
     * Get the configuration data.
37
     *
38
     * @param string[] $config
39
     *
40
     * @throws \InvalidArgumentException
41
     *
42
     * @return array
43
     */
44
    protected function getConfig(array $config)
45
    {
46
        if (0 === count(array_diff(['username', 'private_key'], array_keys($config)))) {
47
            return array_only($config, ['username', 'private_key', 'mode', 'endpoint']);
48
        }
49
50
        throw new \InvalidArgumentException('The TransIP client requires configuration.');
51
    }
52
53
    /**
54
     * Get the TransIP client.
55
     *
56
     * @param string[] $config
57
     *
58
     * @return \TransIP\Client
59
     */
60
    protected function getClient(array $config)
61
    {
62
        return new Client(
63
            $config['username'],
64
            $config['private_key'],
65
            (isset($config['mode'])) ? $config['mode'] : 'readonly',
66
            (isset($config['endpoint'])) ? $config['endpoint'] : 'api.transip.nl'
67
        );
68
    }
69
}
70