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

TransIPFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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