DockerClient::createFromEnv()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 26
ccs 0
cts 17
cp 0
crap 42
rs 8.439
1
<?php
2
3
namespace Docker;
4
5
use Http\Client\HttpClient;
6
use Http\Client\Plugin\ContentLengthPlugin;
7
use Http\Client\Plugin\DecoderPlugin;
8
use Http\Client\Plugin\ErrorPlugin;
9
use Http\Client\Plugin\PluginClient;
10
use Http\Message\MessageFactory\GuzzleMessageFactory;
11
use Http\Client\Socket\Client as SocketHttpClient;
12
use Psr\Http\Message\RequestInterface;
13
14
class DockerClient implements HttpClient
15
{
16
    /**
17
     * @var HttpClient
18
     */
19
    private $httpClient;
20
21
    public function __construct($socketClientOptions = [])
22
    {
23
        $messageFactory = new GuzzleMessageFactory();
24
        $socketClient = new SocketHttpClient($messageFactory, $socketClientOptions);
25
        $lengthPlugin = new ContentLengthPlugin();
26
        $decodingPlugin = new DecoderPlugin();
27
        $errorPlugin = new ErrorPlugin();
28
29
        $this->httpClient = new PluginClient($socketClient, [
30
            $errorPlugin,
31
            $lengthPlugin,
32
            $decodingPlugin
33
        ]);
34
    }
35
36
    /**
37
     * (@inheritdoc}
38
     */
39 11
    public function sendRequest(RequestInterface $request)
40
    {
41 11
        return $this->httpClient->sendRequest($request);
42
    }
43
44
    /**
45
     * @return DockerClient
46
     */
47
    public static function create()
48
    {
49
        return new self([
50
            'remote_socket' => 'unix:///var/run/docker.sock'
51
        ]);
52
    }
53
54
    /**
55
     * Create a docker client from environment variables
56
     *
57
     * @return DockerClient
58
     *
59
     * @throws \RuntimeException Throw exception when invalid environment variables are given
60
     */
61
    public static function createFromEnv()
62
    {
63
        $options = [
64
            'remote_socket' => getenv('DOCKER_HOST') ? getenv('DOCKER_HOST') : 'unix:///var/run/docker.sock'
65
        ];
66
67
        if (getenv('DOCKER_TLS_VERIFY') && getenv('DOCKER_TLS_VERIFY') == 1) {
68
            if (!getenv('DOCKER_CERT_PATH')) {
69
                throw new \RuntimeException('Connection to docker has been set to use TLS, but no PATH is defined for certificate in DOCKER_CERT_PATH docker environnement variable');
70
            }
71
72
            $cafile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'ca.pem';
73
            $certfile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'cert.pem';
74
            $keyfile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'key.pem';
75
            $peername = getenv('DOCKER_PEER_NAME') ? getenv('DOCKER_PEER_NAME') : 'boot2docker';
76
77
            $options['ssl'] = [
78
                'cafile' => $cafile,
79
                'local_cert' => $certfile,
80
                'local_pk' => $keyfile,
81
                'peer_name' => $peername,
82
            ];
83
        }
84
85
        return new self($options);
86
    }
87
}
88