Passed
Push — test ( e55763...46930c )
by Tom
02:53
created

NameBuilder::stepContainerName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 5
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Containers;
6
7
use Ktomk\Pipelines\Project;
8
use UnexpectedValueException;
9
10
/**
11
 * Class NameBuilder
12
 *
13
 * @package Ktomk\Pipelines\Runner\Containers
14
 */
15
class NameBuilder
16
{
17
    /**
18
     * @var Project
19
     */
20
    private $project;
21
22
    /**
23
     * @var string
24
     */
25
    private $role;
26
27
    /**
28
     * @param string $string
29
     * @param string $replacement [optional] defaults to dash "-"
30
     * @param string $fallBack [optional] defaults to empty string
31
     *
32
     * @return string
33
     */
34 18
    public static function slugify($string, $replacement = null, $fallBack = null)
35
    {
36 18
        null === $replacement && $replacement = '-';
37
38
        // all non-allowed characters -> replacement (which is normally a separator "_", "." or "-")
39 18
        $buffer = preg_replace('([^a-zA-Z0-9_.-]+)', (string)$replacement, (string)$string);
40 18
        if (false === $buffer) {
41
            // @codeCoverageIgnoreStart
42
            throw new UnexpectedValueException('regex operation failed');
43
            // @codeCoverageIgnoreEnd
44
        }
45
46
        // multiple separator(s) after each other -> one replacement (which is normally a separator)
47 18
        $buffer = preg_replace('(([_.-])[_.-]+)', (string)$replacement, $buffer);
48
49
        // not starting nor ending with a separator
50 18
        $buffer = trim($buffer, '_.-');
51
52
        // not starting with a number
53 18
        $buffer = preg_replace('(^\d+([_.-]\d+)*)', (string)$replacement, $buffer);
54
55
        // multiple separator(s) after each other -> one replacement (which is normally a separator)
56 18
        $buffer = preg_replace('(([_.-])[_.-]+)', (string)$replacement, $buffer);
57
58
        // not starting nor ending with a separator
59 18
        $buffer = trim($buffer, '_.-');
60
61
        // separator(s) only -> empty string
62 18
        $buffer = preg_replace('(^[_.-]+$)', '', $buffer);
63
64 18
        return '' === (string)$buffer ? (string)$fallBack : (string)$buffer;
65
    }
66
67
    /**
68
     * service container name
69
     *
70
     * example: pipelines.service-redis.pipelines
71
     *              ^    `   ^   `  ^  `   ^
72
     *              |        |      |      |
73
     *              |    "service"  |   project
74
     *           prefix       service name
75
     *
76
     * @param string $prefix
77
     * @param string $serviceName
78
     * @param string $projectName
79
     *
80
     * @return string
81
     */
82 1
    public static function serviceContainerName($prefix, $serviceName, $projectName)
83
    {
84 1
        return self::slugify(
85 1
            sprintf(
86 1
                '%s.service-%s',
87
                $prefix,
88 1
                implode(
89 1
                    '.',
90
                    array(
91 1
                        self::slugify($serviceName, '-', 'unnamed'),
92 1
                        $projectName,
93
                    )
94
                )
95
            )
96
        );
97
    }
98
99
    /**
100
     * step container name
101
     *
102
     * example: pipelines-1.pipeline-features-and-introspection.default.app
103
     *              ^    `^`                  ^                `    ^  ` ^
104
     *              |     |                   |                     |    |
105
     *              | step number        step name           pipeline id |
106
     *           prefix                                                project
107
     *
108
     * @param string $pipelineId
109
     * @param string $stepName
110
     * @param int $stepNumber (step numbers start at one)
111
     * @param string $prefix
112
     * @param string $projectName
113
     *
114
     * @return string
115
     */
116 1
    public static function stepContainerName($pipelineId, $stepName, $stepNumber, $prefix, $projectName)
117
    {
118 1
        return self::slugify(
119 1
            sprintf(
120 1
                '%s-%s',
121
                $prefix,
122 1
                implode(
123 1
                    '.',
124
                    array(
125 1
                        (string)(int)max(1, $stepNumber),
126 1
                        self::slugify($stepName, '-', 'no-name'),
127 1
                        self::slugify($pipelineId, '-', 'null'),
128 1
                        $projectName,
129
                    )
130
                )
131
            ),
132 1
            ''
133
        );
134
    }
135
136
    /**
137
     * @param Project $project
138
     *
139
     * @return self
140
     */
141 1
    public function setProject(Project $project)
142
    {
143 1
        $this->project = $project;
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * @param string $role
150
     *
151
     * @return NameBuilder
152
     */
153 1
    public function setRole($role)
154
    {
155 1
        $this->role = Role::verify($role);
156
157 1
        return $this;
158
    }
159
}
160