Passed
Push — master ( 8cf222...7fa05a )
by Stephen
04:50
created

withImgShieldGlobalParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sfneal\Dependencies\Services;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\Cache;
7
use Sfneal\Caching\Traits\IsCacheable;
8
use Sfneal\Dependencies\Utils\ComposerDependencies;
9
use Sfneal\Helpers\Laravel\LaravelHelpers;
10
11
class DependenciesRepository
12
{
13
    use IsCacheable;
14
15
    /**
16
     * @var array Array of composer or Docker dependencies
17
     */
18
    private $dependencies;
19
20
    /**
21
     * @var bool Use composer.json dependencies as source
22
     */
23
    private $composerDependencies;
24
25
    /**
26
     * @var bool Include composer dev dependencies
27
     */
28
    private $devComposerDependencies;
29
30
    /**
31
     * @var Collection Collection of dependencies retrieved by the `getDependencies()` method
32
     */
33
    private $dependenciesCollection;
34
35
    /**
36
     * @var array|null Array of global Img Shields params to be passed to SVG requests
37
     */
38
    private $imgShieldGlobalParams;
39
40
    /**
41
     * Include Img Shields global params in SVG requests.
42
     *
43
     * @param  array|null  $imgShieldGlobalParams
44
     * @return $this
45
     */
46
    public function withImgShieldGlobalParams(array $imgShieldGlobalParams = null): self
47
    {
48
        $this->imgShieldGlobalParams = $imgShieldGlobalParams;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Retrieve dependencies from the composer.json file & optionally include 'dev' dependencies.
55
     *
56
     * @param  bool  $devComposerDependencies
57
     * @return $this
58
     */
59
    public function fromComposer(bool $devComposerDependencies = false): self
60
    {
61
        $this->composerDependencies = true;
62
        $this->devComposerDependencies = $devComposerDependencies;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Retrieve dependencies from the config file.
69
     *
70
     * @return $this
71
     */
72
    public function fromConfig(): self
73
    {
74
        $this->composerDependencies = false;
75
        $this->dependencies = config('dependencies.dependencies');
76
77
        return $this;
78
    }
79
80
    /**
81
     * Retrieve dependencies from an array.
82
     *
83
     * @param  array  $dependencies
84
     * @return $this
85
     */
86
    public function fromArray(array $dependencies): self
87
    {
88
        $this->composerDependencies = false;
89
        $this->dependencies = $dependencies;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Retrieve a Collection of Dependencies with GitHub, Packagist version & Travis CI build status URLs.
96
     *
97
     * @return Collection
98
     */
99
    public function get(): Collection
100
    {
101
        return Cache::remember(
102
            $this->cacheKey(),
103
            config('dependencies.cache.ttl'),
104
            function () {
105
                $array = [];
106
107
                foreach ($this->getDependencies()->toArray() as $type => $dependencies) {
108
                    foreach ($dependencies as $dependency) {
109
                        $array[] = new DependencyService($dependency, $type, $this->imgShieldGlobalParams);
110
                    }
111
                }
112
113
                return collect($array);
114
            }
115
        );
116
    }
117
118
    /**
119
     * Retrieve a list of dependencies from the config file or by reading the composer.json 'requires' section.
120
     *
121
     * @return Collection
122
     */
123
    private function getDependencies(): Collection
124
    {
125
        if (is_null($this->dependenciesCollection)) {
126
            if ($this->composerDependencies) {
127
                $this->dependenciesCollection = $this->getComposerRequirements();
128
            } else {
129
                $this->dependenciesCollection = $this->getArrayDependencies() ?? $this->getComposerRequirements();
130
            }
131
        }
132
133
        return $this->dependenciesCollection;
134
    }
135
136
    /**
137
     * Retrieve a list of dependencies set in the 'dependencies' config.
138
     *
139
     * @return Collection
140
     */
141
    private function getArrayDependencies(): Collection
142
    {
143
        return collect($this->dependencies);
144
    }
145
146
    /**
147
     * Retrieve an array of composer packages that are required by the composer.json.
148
     *
149
     * @return Collection
150
     */
151
    private function getComposerRequirements(): Collection
152
    {
153
        return collect([
154
            'composer' => (new ComposerDependencies($this->devComposerDependencies))->get()->toArray(),
155
        ]);
156
    }
157
158
    /**
159
     * Retrieve the cache key.
160
     *
161
     * @return string
162
     */
163
    public function cacheKey(): string
164
    {
165
        return config('dependencies.cache.prefix').':'.LaravelHelpers::serializeHash($this->getDependencies()->toArray());
166
    }
167
}
168