Completed
Push — master ( 8f0b18...2dc816 )
by Samuel
37s
created

DnsDockResolver::getDnsByContainerNameAndImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace Dock\Docker\Dns;
4
5
class DnsDockResolver implements ContainerAddressResolver
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10
    public function getDnsByContainerNameAndImage($containerName, $imageName)
11
    {
12
        $imageName = $this->stripTagNameFromImageName($imageName);
13
        $imageName = $this->stripSlashFromImageName($imageName);
14
        $imageName = $this->sanitiseImageName($imageName);
15
16
        return [
17
            $imageName.'.docker',
18
            $containerName.'.'.$imageName.'.docker',
19
        ];
20
    }
21
22
    /**
23
     * @param string $imageName
24
     *
25
     * @return string
26
     */
27
    private function stripTagNameFromImageName($imageName)
28
    {
29
        if (false !== ($position = strpos($imageName, ':'))) {
30
            $imageName = substr($imageName, 0, $position);
31
        }
32
33
        return $imageName;
34
    }
35
    
36
    private function stripSlashFromImageName($imageName)
37
    {
38
        if (false !== ($position = strrpos($imageName, '/'))) {
39
            $imageName = substr($imageName, $position+1 );
40
        }
41
42
        return $imageName;
43
    }
44
45
    private function sanitiseImageName($imageName)
46
    {
47
        return str_replace(['/', '-'], '_', $imageName);
48
    }
49
}
50