StepServiceContainers   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 32
c 1
b 0
f 1
dl 0
loc 73
ccs 30
cts 30
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A obtainNetwork() 0 22 2
A shutdown() 0 20 2
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Containers;
6
7
use Ktomk\Pipelines\File\Pipeline\Step;
8
use Ktomk\Pipelines\Runner\Containers;
9
use Ktomk\Pipelines\Runner\Runner;
10
11
/**
12
 * Class StepServiceContainers
13
 *
14
 * @package Ktomk\Pipelines\Runner\Containers
15
 */
16
class StepServiceContainers
17
{
18
    /**
19
     * @var Step
20
     */
21
    private $step;
22
    /**
23
     * @var Runner
24
     */
25
    private $runner;
26
27 4
    public function __construct(Step $step, Runner $runner)
28
    {
29 4
        $this->step = $step;
30 4
        $this->runner = $runner;
31
    }
32
33
    /**
34
     * run all service containers and obtain network configuration (if any)
35
     *
36
     * @return array docker run options for network (needed if there are services)
37
     *
38
     * @see StepRunner::runNewContainer()
39
     */
40 2
    public function obtainNetwork()
41
    {
42 2
        $step = $this->step;
43
44 2
        $services = (array)$step->getServices()->getDefinitions();
45
46 2
        $network = array();
47
48 2
        $labels = LabelsBuilder::createFromRunner($this->runner);
49
50 2
        foreach ($services as $name => $service) {
51 1
            list(, $network) = Containers::execRunServiceContainer(
52 1
                $this->runner->getExec(),
53
                $service,
54 1
                $this->runner->getEnv()->getResolver(),
55 1
                $this->runner->getRunOpts()->getPrefix(),
56 1
                $this->runner->getProject(),
57 1
                $labels->setRole('service')->toArray()
58
            );
59
        }
60
61 2
        return $network;
62
    }
63
64
    /**
65
     * @param int $status
66
     *
67
     * @return void
68
     */
69 1
    public function shutdown($status)
70
    {
71 1
        $step = $this->step;
72
73 1
        $services = (array)$step->getServices()->getDefinitions();
74
75 1
        foreach ($services as $service) {
76 1
            $name = NameBuilder::serviceContainerName(
77 1
                $this->runner->getRunOpts()->getPrefix(),
78 1
                $service->getName(),
79 1
                $this->runner->getProject()
80
            );
81
82 1
            Containers::execShutdownContainer(
83 1
                $this->runner->getExec(),
84 1
                $this->runner->getStreams(),
85 1
                "/{$name}",
86
                $status,
87 1
                $this->runner->getFlags(),
88 1
                sprintf('keeping service container %s', $name)
89
            );
90
        }
91
    }
92
}
93