Passed
Push — master ( 2bfbe4...3e2aab )
by Stephen
02:57 queued 11s
created

DependenciesRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
eloc 34
c 5
b 1
f 0
dl 0
loc 137
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 6 1
A fromComposer() 0 6 1
A fromConfig() 0 6 1
A getArrayDependencies() 0 9 1
A get() 0 8 1
A getComposerRequirements() 0 3 1
A cacheKey() 0 3 1
A getDependencies() 0 11 3
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
     * Retrieve dependencies from the composer.json file & optionally include 'dev' dependencies.
37
     *
38
     * @param bool $devComposerDependencies
39
     * @return $this
40
     */
41
    public function fromComposer(bool $devComposerDependencies = false): self
42
    {
43
        $this->composerDependencies = true;
44
        $this->devComposerDependencies = $devComposerDependencies;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Retrieve dependencies from the config file.
51
     *
52
     * @return $this
53
     */
54
    public function fromConfig(): self
55
    {
56
        $this->composerDependencies = false;
57
        $this->dependencies = config('dependencies.dependencies');
58
59
        return $this;
60
    }
61
62
    /**
63
     * Retrieve dependencies from an array.
64
     *
65
     * @param array $dependencies
66
     * @return $this
67
     */
68
    public function fromArray(array $dependencies): self
69
    {
70
        $this->composerDependencies = false;
71
        $this->dependencies = $dependencies;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Retrieve a Collection of Dependencies with GitHub, Packagist version & Travis CI build status URLs.
78
     *
79
     * @return Collection
80
     */
81
    public function get(): Collection
82
    {
83
        return Cache::remember(
84
            $this->cacheKey(),
85
            config('dependencies.cache.ttl'),
86
            function () {
87
                return $this->getDependencies()->map(function (string $type, string $dependency) {
88
                    return new DependenciesService($dependency, $type);
89
                });
90
            }
91
        );
92
    }
93
94
    /**
95
     * Retrieve a list of dependencies from the config file or by reading the composer.json 'requires' section.
96
     *
97
     * @return Collection
98
     */
99
    private function getDependencies(): Collection
100
    {
101
        if (is_null($this->dependenciesCollection)) {
102
            if ($this->composerDependencies) {
103
                $this->dependenciesCollection = $this->getComposerRequirements();
104
            } else {
105
                $this->dependenciesCollection = $this->getArrayDependencies() ?? $this->getComposerRequirements();
106
            }
107
        }
108
109
        return $this->dependenciesCollection;
110
    }
111
112
    /**
113
     * Retrieve a list of dependencies set in the 'dependencies' config.
114
     *
115
     * @return Collection
116
     */
117
    private function getArrayDependencies(): Collection
118
    {
119
        // Convert array of dependency type keys & array of dependency values
120
        // to a flat array of dependency keys and type values
121
        return collect($this->dependencies)
122
            ->mapWithKeys(function ($packages, $type) {
123
                return collect($packages)
124
                    ->mapWithKeys(function (string $package) use ($type) {
125
                        return [$package => $type];
126
                    });
127
            });
128
    }
129
130
    /**
131
     * Retrieve an array of composer packages that are required by the composer.json.
132
     *
133
     * @return Collection
134
     */
135
    private function getComposerRequirements(): Collection
136
    {
137
        return (new ComposerDependencies($this->devComposerDependencies))->get();
138
    }
139
140
    /**
141
     * Retrieve the cache key.
142
     *
143
     * @return string
144
     */
145
    public function cacheKey(): string
146
    {
147
        return config('dependencies.cache.prefix').':'.LaravelHelpers::serializeHash($this->getDependencies()->toArray());
148
    }
149
}
150