ContainerManager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 87.88%

Importance

Changes 23
Bugs 2 Features 14
Metric Value
wmc 7
c 23
b 2
f 14
lcom 0
cbo 3
dl 12
loc 63
ccs 29
cts 33
cp 0.8788
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B attachWebsocket() 0 35 3
A attach() 12 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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