Passed
Push — master ( 71afca...9e5fe0 )
by Tom
04:18
created

Flags::createForUtility()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 12
nop 3
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use Ktomk\Pipelines\Utility\CacheOptions;
8
use Ktomk\Pipelines\Utility\KeepOptions;
9
10
/**
11
 * Flags for use in Runner
12
 */
13
class Flags
14
{
15
    /**
16
     * default flags
17
     *
18
     * 19: FLAG_DOCKER_REMOVE (1) | FLAG_DOCKER_KILL (2) | FLAG_SOCKET (16)
19
     */
20
    const FLAGS = 19;
21
22
    /**
23
     * remove docker container after pipeline run
24
     */
25
    const FLAG_DOCKER_REMOVE = 1;
26
27
    /**
28
     * kill running docker container after pipeline run
29
     */
30
    const FLAG_DOCKER_KILL = 2;
31
32
    /**
33
     * use copy to deploy project into pipeline container
34
     */
35
    const FLAG_DEPLOY_COPY = 4; # copy working dir into container
36
37
    /**
38
     * keep pipeline container in case of error (non zero exit status)
39
     */
40
    const FLAG_KEEP_ON_ERROR = 8;
41
42
    /**
43
     * mount docker daemon socket into pipeline container (docker in docker)
44
     */
45
    const FLAG_SOCKET = 16;
46
47
    /**
48
     * do not make use of dependency caches
49
     */
50
    const FLAG_NO_CACHE = 32;
51
52
    /**
53
     * @var int flags bit-mask value
54
     */
55
    public $memory = self::FLAGS;
56
57
    /**
58
     * Map diverse parameters to run flags
59
     *
60
     * @param KeepOptions $keep
61
     * @param string $deployMode
62
     * @param CacheOptions $cache
63
     *
64
     * @return Flags
65
     */
66 3
    public static function createForUtility(KeepOptions $keep, $deployMode, CacheOptions $cache)
67
    {
68 3
        $flagsValue = Flags::FLAGS;
69 3
        if ($keep->errorKeep) {
70 1
            $flagsValue |= Flags::FLAG_KEEP_ON_ERROR;
71 2
        } elseif ($keep->keep) {
72 1
            $flagsValue &= ~(Flags::FLAG_DOCKER_KILL | Flags::FLAG_DOCKER_REMOVE);
73
        }
74
75 3
        $cache->hasCache() || $flagsValue |= Flags::FLAG_NO_CACHE;
76
77 3
        if ('copy' === $deployMode) {
78 3
            $flagsValue |= Flags::FLAG_DEPLOY_COPY;
79
        }
80
81 3
        return new Flags($flagsValue);
82
    }
83
84
    /**
85
     * Flags constructor.
86
     *
87
     * @param int $memory [optional]
88
     */
89 13
    public function __construct($memory = null)
90
    {
91 13
        null === $memory || $this->memory = $memory;
92 13
    }
93
94
    /**
95
     * @return bool
96
     */
97 1
    public function deployCopy()
98
    {
99 1
        return (bool)($this->memory & self::FLAG_DEPLOY_COPY);
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 1
    public function keep()
106
    {
107 1
        return !($this->memory & (self::FLAG_DOCKER_KILL | self::FLAG_DOCKER_REMOVE));
108
    }
109
110
    /**
111
     * @return bool
112
     */
113 1
    public function keepOnError()
114
    {
115 1
        return (bool)($this->memory & self::FLAG_KEEP_ON_ERROR);
116
    }
117
118
    /**
119
     * @return bool
120
     */
121 1
    public function killContainer()
122
    {
123 1
        return (bool)($this->memory & self::FLAG_DOCKER_KILL);
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 1
    public function removeContainer()
130
    {
131 1
        return (bool)($this->memory & self::FLAG_DOCKER_REMOVE);
132
    }
133
134
    /**
135
     * @return bool
136
     */
137 1
    public function reuseContainer()
138
    {
139
        return
140 1
            ($this->memory & self::FLAG_KEEP_ON_ERROR)
141 1
            || !($this->memory & (self::FLAG_DOCKER_KILL | self::FLAG_DOCKER_REMOVE));
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 1
    public function useDockerSocket()
148
    {
149 1
        return (bool)($this->memory & self::FLAG_SOCKET);
150
    }
151
152
    /**
153
     * @return bool
154
     */
155 1
    public function noCache()
156
    {
157 1
        return (bool)($this->memory & self::FLAG_NO_CACHE);
158
    }
159
160
    /**
161
     * @param int $flag
162
     *
163
     * @return bool
164
     */
165 1
    public function flgHas($flag)
166
    {
167 1
        return (bool)($this->memory & $flag);
168
    }
169
}
170