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

ImageRepository::loadConfig()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

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