Completed
Pull Request — master (#147)
by Joel
10:08 queued 07:23
created

DockerClient::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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