Test Failed
Branch feature__set_up_scrutinizer (ea6624)
by Robin
06:04 queued 02:46
created

ImageSetRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 7
eloc 21
dl 0
loc 61
ccs 6
cts 21
cp 0.2857
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addLocation() 0 5 1
A availableImageSets() 0 15 2
A getImageRepository() 0 10 3
A __construct() 0 3 1
1
<?php
2
3
namespace App\Support\Images;
4
5
use App\Support\Contracts\ImageSetRepository as ImageSetRepositoryContract;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Finder\SplFileInfo;
8
9
class ImageSetRepository implements ImageSetRepositoryContract
10
{
11
    protected $locations;
12
13 50
    public function __construct()
14
    {
15 50
        $this->addLocation(base_path('docker'));
16 50
    }
17
18
    /**
19
     * Add a location for the where we may find docker files
20
     *
21
     * @param $location
22
     * @return ImageSetRepository
23
     */
24 50
    public function addLocation($location)
25
    {
26 50
        $this->locations[] = $location;
27
28 50
        return $this;
29
    }
30
31
    /**
32
     * Get an image repository using the most recently added locations first
33
     *
34
     * @param $imageSetName
35
     * @return string
36
     * @throws \Exception
37
     */
38
    public function getImageRepository($imageSetName)
39
    {
40
        foreach (array_reverse($this->locations) as $location) {
41
            $path = $location.'/'.$imageSetName;
42
            if (is_dir($path)) {
43
                return new ImageRepository($path, $imageSetName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new App\Support\I...y($path, $imageSetName) returns the type App\Support\Images\ImageRepository which is incompatible with the documented return type string.
Loading history...
44
            }
45
        }
46
47
        throw new \Exception("Image Set {$imageSetName} not located.");
48
    }
49
50
    /**
51
     * Return a list of the available ImageSets
52
     *
53
     * @return \Illuminate\Support\Collection
54
     */
55
    public function availableImageSets()
56
    {
57
        return collect($this->locations)
58
            ->flatMap(function ($location) {
59
                try {
60
                    return iterator_to_array(
61
                        Finder::create()->in($location)->depth(1)->directories()
62
                    );
63
                } catch (\InvalidArgumentException $e) {
64
                    return null;
65
                }
66
            })->filter()
67
            ->map(function (SplFileInfo $directory) {
68
                return $directory->getRelativePathname();
69
            })->unique();
70
    }
71
}
72