Test Failed
Pull Request — master (#41)
by Keoghan
06:08
created

ImageRepository::loadConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
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
    protected $firstPartyImages = [];
19
20
    protected $thirdPartyImages = [];
21
22
    /**
23
     * ImageRepository constructor.
24 8
     *
25
     * @param $path
26 8
     * @param $name
27 8
     * @throws Exception
28 8
     */
29
    public function __construct($path, $name)
30
    {
31
        $this->path = $path;
32
        $this->name = $name;
33
34
        $this->loadConfig();
35
    }
36
37 4
    /**
38
     * Load the configuration file for the image set
39 4
     *
40
     * @throws Exception
41 4
     */
42
    protected function loadConfig()
43 4
    {
44
        try {
45
            $config = json_decode(file_get_contents($this->path.'/config.json'));
46 4
47
            if (json_last_error() !== JSON_ERROR_NONE) {
48
                throw new \Exception(json_last_error_msg());
49
            }
50
51
        } catch (\Exception $e) {
52
            throw new \Exception("Failed loading config for image set '{$this->name}'");
53
        }
54 3
55
        $this->firstPartyImages = $config->firstParty ?? [];
56
        $this->thirdPartyImages = $config->thirdParty ?? [];
57 3
    }
58 3
59 3
    /**
60 3
     * Get the docker images that are pulled on install. A custom image set name may be specified.
61
     *
62
     * @throws Exception
63
     *
64
     * @return array
65
     */
66
    public function firstParty()
67
    {
68
        return collect($this->firstPartyImages)
69
            ->mapWithKeys(function ($version, $name) {
70
                return [new Image($name.':'.$version, $this->getDockerContext().$name)];
71 2
            }, [])->toArray();
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Support\Collection::mapWithKeys() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            ->/** @scrutinizer ignore-call */ mapWithKeys(function ($version, $name) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
    }
73 2
74
    /**
75
     * The third party docker images.
76
     *
77
     * @return array
78
     */
79
    public function thirdParty()
80
    {
81 1
        return collect($this->thirdPartyImages)
82
            ->map(function ($image) {
83 1
                return new Image($image);
84
            })->toArray();
85
    }
86
87
    /**
88
     * Return a full listing of images.
89
     *
90
     * @throws Exception
91 3
     *
92
     * @return array
93 3
     */
94
    public function all()
95
    {
96
        return array_merge($this->firstParty(), $this->thirdParty());
97
    }
98
99
    /**
100
     * Return Docker context path
101
     *
102
     * @return string
103
     */
104
    public function getDockerContext()
105
    {
106 2
        return $this->path.'/docker/';
107
    }
108 2
109 2
    /**
110
     * Return the path.
111 2
     *
112
     * @return string
113
     */
114
    public function getPath()
115
    {
116 2
        return $this->path;
117 2
    }
118
119
    /**
120
     * Return the name.
121
     *
122
     * @return string
123
     */
124
    public function getName()
125
    {
126
        return $this->name;
127
    }
128 4
129
    /**
130 4
     * Find the image for a given service.
131
     *
132
     * @param $service
133
     * @param bool $firstPartyOnly
134
     *
135
     * @throws Exception
136
     *
137
     * @return array
138
     */
139
    public function findByServiceName($service, $firstPartyOnly = false)
140
    {
141
        $service = preg_replace('/[^a-zA-Z0-9\-\_]/', '-', $service);
142
        $images = $firstPartyOnly ? $this->firstParty() : $this->all();
143
144
        if (!$service) {
145
            return $images;
146
        }
147
148
        return array_values(array_filter($images, function (Image $image) use ($service) {
149
            return strpos($image->getName(), $service) !== false;
150
        }));
151
    }
152
}
153