NameBuilder::stepContainerName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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