Completed
Push — test ( d8d9f2...ee908a )
by Tom
14:20 queued 28s
created

StepServiceContainers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A obtainNetwork() 0 19 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 4
    }
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
        foreach ($services as $name => $service) {
49 1
            list(, $network) = Containers::execRunServiceContainer(
50 1
                $this->runner->getExec(),
51 1
                $service,
52 1
                $this->runner->getEnv()->getResolver(),
53 1
                $this->runner->getRunOpts()->getPrefix(),
54 1
                $this->runner->getProject()
55
            );
56
        }
57
58 2
        return $network;
59
    }
60
61
    /**
62
     * @param int $status
63
     *
64
     * @return void
65
     */
66 1
    public function shutdown($status)
67
    {
68 1
        $step = $this->step;
69
70 1
        $services = (array)$step->getServices()->getDefinitions();
71
72 1
        foreach ($services as $name => $service) {
73 1
            $name = NameBuilder::serviceContainerName(
74 1
                $this->runner->getRunOpts()->getPrefix(),
75 1
                $service->getName(),
76 1
                $this->runner->getProject()
77
            );
78
79 1
            Containers::execShutdownContainer(
80 1
                $this->runner->getExec(),
81 1
                $this->runner->getStreams(),
82 1
                "/${name}",
83 1
                $status,
84 1
                $this->runner->getFlags(),
85 1
                sprintf('keeping service container %s', $name)
86
            );
87
        }
88
    }
89
}
90