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

DnsDockResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 0
dl 0
loc 45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDnsByContainerNameAndImage() 0 11 1
A sanitiseImageName() 0 4 1
A stripTagNameFromImageName() 0 8 2
A stripSlashFromImageName() 0 8 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