Passed
Pull Request — master (#41)
by Keoghan
03:03
created

ImageRepository::loadConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3.1406
1
<?php
2
3
namespace App\Support\Images;
4
5
use App\Support\Contracts\ImageRepository as ImageRepositoryContract;
6
use Exception;
7
8
class ImageRepository implements ImageRepositoryContract
9
{
10
    /** @var string */
11
    protected $path;
12
13
    /** @var string */
14
    protected $name;
15
16
    protected $firstPartyImages = [];
17
18
    protected $thirdPartyImages = [];
19
20
    /**
21
     * ImageRepository constructor.
22
     *
23
     * @param $path
24
     * @param $name
25
     *
26
     * @throws Exception
27
     */
28 8
    public function __construct($path, $name)
29
    {
30 8
        $this->path = $path;
31 8
        $this->name = $name;
32
33 8
        $this->loadConfig();
34 8
    }
35
36
    /**
37
     * Load the configuration file for the image set.
38
     *
39
     * @throws Exception
40
     */
41 8
    protected function loadConfig()
42
    {
43
        try {
44 8
            $config = json_decode(file_get_contents($this->path.'/config.json'));
45
46 8
            if (json_last_error() !== JSON_ERROR_NONE) {
47 8
                throw new \Exception(json_last_error_msg());
48
            }
49
        } catch (\Exception $e) {
50
            throw new \Exception("Failed loading config for image set '{$this->name}'");
51
        }
52
53 8
        $this->firstPartyImages = (array) $config->firstParty ?? [];
54 8
        $this->thirdPartyImages = (array) $config->thirdParty ?? [];
55 8
    }
56
57
    /**
58
     * Get the docker images that are pulled on install. A custom image set name may be specified.
59
     *
60
     * @throws Exception
61
     *
62
     * @return array
63
     */
64 4
    public function firstParty()
65
    {
66 4
        return collect($this->firstPartyImages)
67
            ->map(function ($version, $name) {
68 4
                return new Image($this->name.'-'.$name.':'.$version, $this->getDockerContext().$name);
69 4
            })->values()->toArray();
70
    }
71
72
    /**
73
     * The third party docker images.
74
     *
75
     * @return array
76
     */
77 3
    public function thirdParty()
78
    {
79 3
        return collect($this->thirdPartyImages)
80
            ->map(function ($image) {
81 3
                return new Image($image);
82 3
            })->toArray();
83
    }
84
85
    /**
86
     * Return a full listing of images.
87
     *
88
     * @throws Exception
89
     *
90
     * @return array
91
     */
92 2
    public function all()
93
    {
94 2
        return array_merge($this->firstParty(), $this->thirdParty());
95
    }
96
97
    /**
98
     * Return Docker context path.
99
     *
100
     * @return string
101
     */
102 4
    public function getDockerContext()
103
    {
104 4
        return $this->path.'/docker/';
105
    }
106
107
    /**
108
     * Return the path.
109
     *
110
     * @return string
111
     */
112 1
    public function getPath()
113
    {
114 1
        return $this->path;
115
    }
116
117
    /**
118
     * Return the name.
119
     *
120
     * @return string
121
     */
122 3
    public function getName()
123
    {
124 3
        return $this->name;
125
    }
126
127
    /**
128
     * Find the image for a given service.
129
     *
130
     * @param $service
131
     * @param bool $firstPartyOnly
132
     *
133
     * @throws Exception
134
     *
135
     * @return array
136
     */
137 2
    public function findByServiceName($service, $firstPartyOnly = false)
138
    {
139 2
        $service = preg_replace('/[^a-zA-Z0-9\-\_]/', '-', $service);
140 2
        $images = $firstPartyOnly ? $this->firstParty() : $this->all();
141
142 2
        if (!$service) {
143
            return $images;
144
        }
145
146
        return array_values(array_filter($images, function (Image $image) use ($service) {
147 2
            return strpos($image->getName(), $service) !== false;
148 2
        }));
149
    }
150
}
151