ContainerManager::attachWebsocket()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3.004

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 25
c 1
b 0
f 1
nc 3
nop 3
dl 0
loc 35
ccs 24
cts 26
cp 0.9231
crap 3.004
rs 8.8571
1
<?php
2
3
namespace Docker\Manager;
4
5
use Docker\API\Resource\ContainerResource;
6
use Docker\Stream\AttachWebsocketStream;
7
use Docker\Stream\DockerRawStream;
8
use Joli\Jane\OpenApi\Client\QueryParam;
9
10
class ContainerManager extends ContainerResource
11
{
12
    const FETCH_STREAM = 'stream';
13
14
    /**
15
     * {@inheritdoc}
16
     *
17
     * @return \Psr\Http\Message\ResponseInterface|DockerRawStream
18
     */
19 1 View Code Duplication
    public function attach($id, $parameters = [], $fetch = self::FETCH_STREAM)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21 1
        $response = parent::attach($id, $parameters, $fetch);
22
23 1
        if ($response->getStatusCode() == 200 && DockerRawStream::HEADER == $response->getHeaderLine('Content-Type')) {
24 1
            if ($fetch == self::FETCH_STREAM) {
25 1
                return new DockerRawStream($response->getBody());
26
            }
27
        }
28
29
        return $response;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @return \Psr\Http\Message\ResponseInterface|AttachWebsocketStream
36
     */
37 1
    public function attachWebsocket($id, $parameters = [], $fetch = self::FETCH_STREAM)
38
    {
39 1
        $queryParam = new QueryParam();
40 1
        $queryParam->setDefault('logs', null);
41 1
        $queryParam->setDefault('stream', null);
42 1
        $queryParam->setDefault('stdin', null);
43 1
        $queryParam->setDefault('stdout', null);
44 1
        $queryParam->setDefault('stderr', null);
45
46 1
        $url      = '/containers/{id}/attach/ws';
47 1
        $url      = str_replace('{id}', $id, $url);
48 1
        $url      = $url . ('?' . $queryParam->buildQueryString($parameters));
49
50 1
        $headers  = array_merge([
51 1
            'Host' => 'localhost',
52 1
            'Origin' => 'php://docker-php',
53 1
            'Upgrade' => 'websocket',
54 1
            'Connection' => 'Upgrade',
55 1
            'Sec-WebSocket-Version' => '13',
56 1
            'Sec-WebSocket-Key' => base64_encode(uniqid()),
57 1
        ], $queryParam->buildHeaders($parameters));
58
59 1
        $body     = $queryParam->buildFormDataString($parameters);
60
61 1
        $request  = $this->messageFactory->createRequest('GET', $url, $headers, $body);
62 1
        $response = $this->httpClient->sendRequest($request);
63
64 1
        if ($response->getStatusCode() == 101) {
65 1
            if ($fetch == self::FETCH_STREAM) {
66 1
                return new AttachWebsocketStream($response->getBody());
67
            }
68
        }
69
70
        return $response;
71
    }
72
}
73