Test Failed
Pull Request — master (#31)
by Keoghan
03:21
created

ImageRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
eloc 24
dl 0
loc 121
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A thirdParty() 0 7 1
A findByServiceName() 0 11 3
A getImageName() 0 3 1
A getPath() 0 3 1
A firstParty() 0 10 2
A getName() 0 3 1
A all() 0 3 1
1
<?php
2
3
namespace App\Support\Images;
4
5
use App\Support\Contracts\ImageRepository as ImageRepositoryContract;
6
use Exception;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
class ImageRepository implements ImageRepositoryContract
11
{
12
    /** @var string */
13
    protected $path;
14
15
    /** @var string */
16
    protected $name;
17
18
    /**
19
     * ImageRepository constructor.
20
     *
21
     * @param $path
22
     * @param $name
23
     */
24
    public function __construct($path, $name)
25
    {
26
        $this->path = $path;
27
        $this->name = $name;
28
    }
29
30
    /**
31
     * Get the docker images that are pulled on install. A custom image set name may be specified.
32
     *
33
     * @throws Exception
34
     *
35
     * @return array
36
     */
37
    public function firstParty()
38
    {
39
        $images = [];
40
41
        foreach ((new Finder())->in($this->path)->directories() as $directory) {
42
            /* @var $directory \Symfony\Component\Finder\SplFileInfo */
43
            $images[] = new Image($this->getImageName($directory, $this->name), $directory->getRealPath());
44
        }
45
46
        return $images;
47
    }
48
49
    /**
50
     * The third party docker images.
51
     *
52
     * @return array
53
     */
54
    public function thirdParty()
55
    {
56
        return [
57
            new Image('mysql:5.7'),
58
            new Image('redis:alpine'),
59
            new Image('andyshinn/dnsmasq'),
60
            new Image('mailhog/mailhog:v1.0.0'),
61
        ];
62
    }
63
64
    /**
65
     * Return a full listing of images.
66
     *
67
     * @throws Exception
68
     *
69
     * @return array
70
     */
71
    public function all()
72
    {
73
        return array_merge($this->firstParty(), $this->thirdParty());
74
    }
75
76
    /**
77
     * Return the path.
78
     *
79
     * @return string
80
     */
81
    public function getPath()
82
    {
83
        return $this->path;
84
    }
85
86
    /**
87
     * Return the name.
88
     *
89
     * @return string
90
     */
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * Find the image for a given service.
98
     *
99
     * @param $service
100
     * @param bool $firstParty
101
     *
102
     * @throws Exception
103
     *
104
     * @return array
105
     */
106
    public function findByServiceName($service, $firstParty = false)
107
    {
108
        $service = preg_replace('/[^a-zA-Z0-9\-\_]/', '-', $service);
109
        $images = $firstParty ? $this->firstParty() : $this->all();
110
111
        if (!$service) {
112
            return $images;
113
        }
114
115
        return array_values(array_filter($images, function (Image $image) use ($service) {
116
            return strpos($image->name, $service) !== false;
117
        }));
118
    }
119
120
    /**
121
     * Get the name of the docker image from the directory and image set name.
122
     *
123
     * @param string      $imageSetName
124
     * @param SplFileInfo $dir
125
     *
126
     * @return string
127
     */
128
    private function getImageName(SplFileInfo $dir, $imageSetName)
129
    {
130
        return $imageSetName.'-'.$dir->getFileName().':latest';
131
    }
132
}
133