Test Failed
Pull Request — master (#31)
by Keoghan
04:32 queued 01:26
created

ImageRepository::findByServiceName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3.0416
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 8
    public function __construct($path, $name)
25
    {
26 8
        $this->path = $path;
27 8
        $this->name = $name;
28 8
    }
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 4
    public function firstParty()
38
    {
39 4
        $images = [];
40
41 4
        foreach ((new Finder())->in($this->path)->directories() as $directory) {
42
            /* @var $directory \Symfony\Component\Finder\SplFileInfo */
43 4
            $images[] = new Image($this->getImageName($directory, $this->name), $directory->getRealPath());
44
        }
45
46 4
        return $images;
47
    }
48
49
    /**
50
     * The third party docker images.
51
     *
52
     * @return array
53
     */
54 3
    public function thirdParty()
55
    {
56
        return [
57 3
            new Image('mysql:5.7'),
58 3
            new Image('redis:alpine'),
59 3
            new Image('andyshinn/dnsmasq'),
60 3
            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 2
    public function all()
72
    {
73 2
        return array_merge($this->firstParty(), $this->thirdParty());
74
    }
75
76
    /**
77
     * Return the path.
78
     *
79
     * @return string
80
     */
81 1
    public function getPath()
82
    {
83 1
        return $this->path;
84
    }
85
86
    /**
87
     * Return the name.
88
     *
89
     * @return string
90
     */
91 3
    public function getName()
92
    {
93 3
        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 2
    public function findByServiceName($service, $firstParty = false)
107
    {
108 2
        $service = preg_replace('/[^a-zA-Z0-9\-\_]/', '-', $service);
109 2
        $images = $firstParty ? $this->firstParty() : $this->all();
110
111 2
        if (!$service) {
112
            return $images;
113
        }
114
115
        return array_values(array_filter($images, function (Image $image) use ($service) {
116 2
            return strpos($image->getName(), $service) !== false;
117 2
        }));
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 4
    private function getImageName(SplFileInfo $dir, $imageSetName)
129
    {
130 4
        return $imageSetName.'-'.$dir->getFileName().':latest';
131
    }
132
}
133