Passed
Push — master ( 48c4ea...d64961 )
by Tom
02:50
created

ServiceOptions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
nc 1
nop 7
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use Ktomk\Pipelines\Cli\Args;
8
use Ktomk\Pipelines\Cli\ArgsException;
9
use Ktomk\Pipelines\Cli\Exec;
10
use Ktomk\Pipelines\Cli\Streams;
11
use Ktomk\Pipelines\File\File;
12
use Ktomk\Pipelines\Runner\Directories;
13
use Ktomk\Pipelines\Runner\Env;
14
use Ktomk\Pipelines\Runner\RunOpts;
15
use Ktomk\Pipelines\Runner\StepContainer;
16
17
/**
18
 * Class ServiceOptions
19
 *
20
 * --run-service <service> - run a service (attached) and exit after service exited
21
 *
22
 * for trouble-shooting/integrating a service quickly
23
 *
24
 * @package Ktomk\Pipelines\Utility
25
 */
26
class ServiceOptions implements Runnable
27
{
28
    /**
29
     * @var Args
30
     */
31
    private $args;
32
    /**
33
     * @var Streams
34
     */
35
    private $streams;
36
    /**
37
     * @var File
38
     */
39
    private $file;
40
    /**
41
     * @var Exec
42
     */
43
    private $exec;
44
    /**
45
     * @var Env
46
     */
47
    private $env;
48
    /**
49
     * @var RunOpts
50
     */
51
    private $runOpts;
52
    /**
53
     * @var Directories
54
     */
55
    private $directories;
56
57
    /**
58
     * @param Args $args
59
     * @param Streams $streams
60
     * @param File $file
61
     *
62
     * @return ServiceOptions
63
     */
64 4
    public static function bind(
65
        Args $args,
66
        Streams $streams,
67
        File $file,
68
        Exec $exec,
69
        Env $env,
70
        RunOpts $runOpts,
71
        Directories $directories
72
    )
73
    {
74 4
        return new self($args, $streams, $file, $exec, $env, $runOpts, $directories);
75
    }
76
77
    /**
78
     * ServiceOptions constructor.
79
     *
80
     * @param Args $args
81
     * @param Streams $streams
82
     * @param File $file
83
     * @param Exec $exec
84
     * @param Env $env
85
     * @param RunOpts $runOpts
86
     * @param Directories $directories
87
     */
88 4
    public function __construct(
89
        Args $args,
90
        Streams $streams,
91
        File $file,
92
        Exec $exec,
93
        Env $env,
94
        RunOpts $runOpts,
95
        Directories $directories
96
    )
97
    {
98 4
        $this->args = $args;
99 4
        $this->streams = $streams;
100 4
        $this->file = $file;
101 4
        $this->exec = $exec;
102 4
        $this->env = $env;
103 4
        $this->runOpts = $runOpts;
104 4
        $this->directories = $directories;
105 4
    }
106
107
    /**
108
     * @throws ArgsException
109
     * @throws StatusException
110
     *
111
     * @return void
112
     */
113 4
    public function run()
114
    {
115 4
        if ('' === $nameOfService = $this->args->getStringOptionArgument('service', '')) {
116 1
            return;
117
        }
118
119 2
        $service = $this->file->getDefinitions()->getServices()->getByName($nameOfService);
120 2
        if (null === $service) {
121 1
            throw new ArgsException(sprintf('undefined service: %s', $nameOfService), 1);
122
        }
123
124 1
        $this->streams->out(sprintf("starting service %s ...\n", $nameOfService));
125
126 1
        list($status) = StepContainer::execRunServiceContainerAttached(
127 1
            $this->exec,
128 1
            $service,
129 1
            $this->env->getResolver(),
130 1
            $this->runOpts->getPrefix(),
131 1
            $this->env->getValue('BITBUCKET_REPO_SLUG') ?: $this->directories->getName()
132
        );
133
134 1
        throw new StatusException('', $status);
135
    }
136
}
137