ServiceManager::stop()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 13
nc 5
nop 2
1
<?php
2
3
namespace Joli\JoliCi;
4
5
use Docker\API\Model\ContainerConfig;
6
use Docker\Container as DockerContainer;
7
use Docker\Docker;
8
use Docker\Exception\ImageNotFoundException;
9
use Docker\Exception\UnexpectedStatusCodeException;
10
use Docker\Manager\ImageManager;
11
use Http\Client\Plugin\Exception\ClientErrorException;
12
use Psr\Log\LoggerInterface;
13
14
class ServiceManager
15
{
16
    private $docker;
17
18
    private $logger;
19
20
    public function __construct(Docker $docker, LoggerCallback $logger)
21
    {
22
        $this->docker = $docker;
23
        $this->logger = $logger;
24
    }
25
26
    /**
27
     * Start services for a Job
28
     *
29
     * @param Job $build
30
     */
31
    public function start(Job $build)
32
    {
33
        foreach ($build->getServices() as $service) {
34
            try {
35
                $this->docker->getImageManager()->find(sprintf('%s:%s', $service->getRepository(), $service->getTag()));
36
            } catch (ClientErrorException $e) {
37
                if ($e->getResponse()->getStatusCode() == 404) {
38
                    $buildStream = $this->docker->getImageManager()->create(null, [
39
                        'fromImage' => sprintf('%s:%s', $service->getRepository(), $service->getTag())
40
                    ], ImageManager::FETCH_STREAM);
41
42
                    $buildStream->onFrame($this->logger->getBuildCallback());
0 ignored issues
show
Bug introduced by
The method onFrame does only exist in Docker\Stream\CreateImageStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
                    $buildStream->wait();
0 ignored issues
show
Bug introduced by
The method wait does only exist in Docker\Stream\CreateImageStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
44
                } else {
45
                    throw $e;
46
                }
47
            }
48
49
            $serviceConfig = $service->getConfig();
50
            $containerConfig = new ContainerConfig();
51
            $containerConfig->setImage(sprintf('%s:%s', $service->getRepository(), $service->getTag()));
52
            $containerConfig->setLabels([
0 ignored issues
show
Documentation introduced by
array('com.jolici.container=true') is of type array<integer,string,{"0":"string"}>, but the function expects a null|object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
                'com.jolici.container=true'
54
            ]);
55
56
            if (isset($serviceConfig['Env'])) {
57
                $containerConfig->setEnv($serviceConfig['Env']);
58
            }
59
60
            $containerCreateResult = $this->docker->getContainerManager()->create($containerConfig);
61
            $this->docker->getContainerManager()->start($containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
            $service->setContainer($containerCreateResult->getId());
63
        }
64
    }
65
66
    /**
67
     * Stop services for a Job and reinit volumes
68
     *
69
     * @param Job $job     The job to stop services
70
     * @param int $timeout Timeout to wait before killing the service
71
     */
72
    public function stop(Job $job, $timeout = 10)
73
    {
74
        foreach ($job->getServices() as $service) {
75
            if ($service->getContainer()) {
76
                try {
77
                    $this->docker->getContainerManager()->stop($service->getContainer(), [
78
                        't' => $timeout
79
                    ]);
80
                } catch (ClientErrorException $e) {
81
                    if ($e->getResponse()->getStatusCode() != 304) {
82
                        throw $e;
83
                    }
84
                }
85
86
                $this->docker->getContainerManager()->remove($service->getContainer(), [
87
                    'v' => true,
88
                    'force' => true
89
                ]);
90
91
                $service->setContainer(null);
92
            }
93
        }
94
    }
95
}
96