ImageSetRepository::addLocation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Support\Images;
4
5
use App\Support\Contracts\ImageSetRepository as ImageSetRepositoryContract;
6
use Illuminate\Foundation\Application;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
class ImageSetRepository implements ImageSetRepositoryContract
11
{
12
    protected $locations = [];
13
14
    /**
15
     * ImageSetRepository constructor.
16
     * Accepts a set of locations, or a singular one.
17
     *
18
     * @param $location
19
     */
20 210
    public function __construct($location)
21
    {
22 210
        $this->addLocation($location);
23
    }
24
25
    /**
26
     * Add a location for the where we may find docker files.
27
     *
28
     * @param $location
29
     *
30
     * @return ImageSetRepository
31
     */
32 210
    public function addLocation($location)
33
    {
34 210
        if (!is_array($location)) {
35 3
            $location = [$location];
36
        }
37
38 210
        $this->locations = array_unique(array_merge($this->locations, $location));
39
40 210
        return $this;
41
    }
42
43
    /**
44
     * Get an image repository using the most recently added locations first.
45
     *
46
     * @param $imageSetName
47
     *
48
     * @throws \Exception
49
     *
50
     * @return ImageRepository
51
     */
52 9
    public function getImageRepository($imageSetName)
53
    {
54 9
        foreach (array_reverse($this->locations) as $location) {
55 9
            $path = $location.'/'.$imageSetName;
56 9
            if (is_dir($path)) {
57 9
                return new ImageRepository($path);
58
            }
59
        }
60
61
        throw new \Exception("Image Set {$imageSetName} not located.");
62
    }
63
64
    /**
65
     * Return a list of the available ImageSets.
66
     *
67
     * @return \Illuminate\Support\Collection
68
     */
69 210
    public function availableImageSets()
70
    {
71 210
        return collect($this->locations)
72 210
            ->flatMap(function ($location) {
73
                try {
74 210
                    return iterator_to_array(
75 210
                        Finder::create()->in($location)->depth(1)->directories()
76
                    );
77 210
                } catch (\InvalidArgumentException $e) {
78 210
                    return;
79
                }
80 210
            })->filter()
81 210
            ->map(function (SplFileInfo $directory) {
82 210
                return $directory->getRelativePathname();
83 210
            })->unique();
84
    }
85
86
    /**
87
     * Register the view namespaces for the image sets.
88
     *
89
     * @param $app
90
     */
91 210
    public function registerViewNamespaces(Application $app)
92
    {
93 210
        foreach ($this->availableImageSets() as $path => $namespace) {
94 210
            $app['view']->addNamespace($namespace, $path.'/docker_compose');
95
        }
96
    }
97
}
98