Passed
Push — test ( fc64dd...aa074c )
by Tom
03:00
created

NameBuilder::setRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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