Passed
Push — master ( 22f313...2b0877 )
by Stephen
07:14 queued 02:21
created

DependencyService::node()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Dependencies\Services;
4
5
use Sfneal\Dependencies\Utils\DependencyUrl;
6
use Sfneal\Dependencies\Utils\GithubUrl;
7
use Sfneal\Dependencies\Utils\ImgShieldsUrl;
8
use Sfneal\Dependencies\Utils\Url;
9
10
class DependencyService
11
{
12
    /**
13
     * @var string[] Array of supported dependency types
14
     */
15
    private const DEPENDENCY_TYPES = [
16
        'composer',
17
        'docker',
18
        'python',
19
        'node',
20
    ];
21
22
    /**
23
     * @var string Name of the dependency
24
     */
25
    public $package;
26
27
    /**
28
     * @var string Type of Dependency ('composer', 'docker', 'python' or 'node')
29
     */
30
    public $type;
31
32
    /**
33
     * @var string Name of the GitHub package.
34
     */
35
    public $githubRepo;
36
37
    /**
38
     * @var string Name of the project without the GitHub user prefix (used for Python projects).
39
     */
40
    public $project;
41
42
    /**
43
     * @var array|null Array of global Img Shields params to be passed to SVG requests
44
     */
45
    private $imgShieldGlobals;
46
47
    /**
48
     * DependenciesService constructor.
49
     *
50
     * @param  string  $package
51
     * @param  string  $type
52
     * @param  array|null  $imgShieldGlobals
53
     */
54
    public function __construct(string $package, string $type = 'composer', array $imgShieldGlobals = null)
55
    {
56
        $this->package = $package;
57
        $this->setGitHubRepo($package);
58
        $this->setType($type);
59
        $this->setProject($package);
60
        $this->imgShieldGlobals = $imgShieldGlobals;
61
    }
62
63
    /**
64
     * Retrieve a GitHub URL for a dependency.
65
     *
66
     * @return DependencyUrl|GithubUrl
67
     */
68
    public function gitHub(): GithubUrl
69
    {
70
        return new GithubUrl($this->githubRepo, $this->imgShieldGlobals);
71
    }
72
73
    /**
74
     * Retrieve a Travis CI build status SVG URL for a dependency.
75
     *
76
     * @return DependencyUrl
77
     */
78
    public function travis(): DependencyUrl
79
    {
80
        return new DependencyUrl(
81
            Url::from("app.travis-ci.com/{$this->githubRepo}"),
82
            Url::from("app.travis-ci.com/{$this->githubRepo}.svg")
83
                ->withParams([
84
                    'branch' => 'master',
85
                ]),
86
        );
87
    }
88
89
    /**
90
     * Retrieve the Dependencies latest version.
91
     *
92
     * @return DependencyUrl
93
     */
94
    public function version(): DependencyUrl
95
    {
96
        switch ($this->type) {
97
            // Docker
98
            case 'docker':
99
                return $this->docker();
100
101
            // Python
102
            case 'python':
103
                return $this->pypi();
104
105
            // Python
106
            case 'node':
107
                return $this->node();
108
109
            // PHP
110
            default:
111
                return $this->packagist();
112
        }
113
    }
114
115
    /**
116
     * Retrieve date of the last GitHub commit.
117
     *
118
     * @return DependencyUrl
119
     */
120
    public function lastCommit(): DependencyUrl
121
    {
122
        return new DependencyUrl(
123
            Url::from("github.com/{$this->githubRepo}"),
124
            ImgShieldsUrl::from("github/last-commit/{$this->githubRepo}")
125
                ->withGlobalParams($this->imgShieldGlobals)
126
        );
127
    }
128
129
    /**
130
     * Retrieve number of open issues.
131
     *
132
     * @return DependencyUrl
133
     */
134
    public function openIssues(): DependencyUrl
135
    {
136
        return new DependencyUrl(
137
            Url::from("github.com/{$this->githubRepo}/issues"),
138
            ImgShieldsUrl::from("github/issues-raw/{$this->githubRepo}")
139
                ->withGlobalParams($this->imgShieldGlobals)
140
        );
141
    }
142
143
    /**
144
     * Retrieve number of closed issues.
145
     *
146
     * @return DependencyUrl
147
     */
148
    public function closedIssues(): DependencyUrl
149
    {
150
        return new DependencyUrl(
151
            Url::from("github.com/{$this->githubRepo}/issues")
152
                ->withParams([
153
                    'q' => 'is%3Aissue+is%3Aclosed',
154
                ]),
155
            ImgShieldsUrl::from("github/issues-closed-raw/{$this->githubRepo}")
156
                ->withParams([
157
                    'color' => 'red',
158
                ])
159
                ->withGlobalParams($this->imgShieldGlobals),
160
        );
161
    }
162
163
    /**
164
     * Retrieve number of open pull requests.
165
     *
166
     * @return DependencyUrl
167
     */
168
    public function openPullRequests(): DependencyUrl
169
    {
170
        return new DependencyUrl(
171
            Url::from("github.com/{$this->githubRepo}/pulls"),
172
            ImgShieldsUrl::from("github/issues-pr-raw/{$this->githubRepo}")
173
                ->withGlobalParams($this->imgShieldGlobals)
174
        );
175
    }
176
177
    /**
178
     * Retrieve number of closed pull requests.
179
     *
180
     * @return DependencyUrl
181
     */
182
    public function closedPullRequests(): DependencyUrl
183
    {
184
        return new DependencyUrl(
185
            Url::from("github.com/{$this->githubRepo}/pulls")
186
                ->withParams([
187
                    'q' => 'is%3Aissue+is%3Aclosed',
188
                ]),
189
            ImgShieldsUrl::from("github/issues-pr-closed-raw/{$this->githubRepo}")
190
                ->withParams([
191
                    'color' => 'red',
192
                ])
193
                ->withGlobalParams($this->imgShieldGlobals)
194
        );
195
    }
196
197
    /**
198
     * Retrieve a Packagist versions SVG URL for a dependency.
199
     *
200
     * @return DependencyUrl
201
     */
202
    private function packagist(): DependencyUrl
203
    {
204
        return new DependencyUrl(
205
            Url::from("packagist.org/packages/{$this->package}"),
206
            ImgShieldsUrl::from("packagist/v/{$this->package}.svg")
207
                ->withGlobalParams($this->imgShieldGlobals)
208
        );
209
    }
210
211
    /**
212
     * Retrieve the latest Docker image tag for a dependency.
213
     *
214
     * @return DependencyUrl
215
     */
216
    private function docker(): DependencyUrl
217
    {
218
        return new DependencyUrl(
219
            Url::from("hub.docker.com/r/{$this->package}"),
220
            ImgShieldsUrl::from("docker/v/{$this->package}.svg")
221
                ->withParams([
222
                    'sort' => 'semver',
223
                ])
224
                ->withGlobalParams($this->imgShieldGlobals)
225
        );
226
    }
227
228
    /**
229
     * Retrieve the latest PyPi version a dependency.
230
     *
231
     * @return DependencyUrl
232
     */
233
    private function pypi(): DependencyUrl
234
    {
235
        $project = explode('/', $this->package)[1];
236
237
        return new DependencyUrl(
238
            Url::from("pypi.org/project/{$project}"),
239
            ImgShieldsUrl::from("pypi/v/{$project}.svg")
240
                ->withGlobalParams($this->imgShieldGlobals)
241
        );
242
    }
243
244
    /**
245
     * Retrieve a Node versions SVG URL for a dependency.
246
     *
247
     * @return DependencyUrl
248
     */
249
    private function node(): DependencyUrl
250
    {
251
        $project = explode('/', $this->package)[1];
252
253
        return new DependencyUrl(
254
            Url::from("npmjs.com/package/{$project}"),
255
            ImgShieldsUrl::from("npm/v/{$project}.svg")
256
                ->withGlobalParams($this->imgShieldGlobals)
257
        );
258
    }
259
260
    /**
261
     * Retrieve the GitHub package name with alias replacement.
262
     *
263
     * @param  string  $fullPackageName
264
     * @return void
265
     */
266
    private function setGitHubRepo(string $fullPackageName): void
267
    {
268
        [$user, $package] = explode('/', $fullPackageName);
269
270
        // Replace GitHub username with alias if one is provided
271
        if (array_key_exists($user, config('dependencies.github_alias'))) {
272
            $this->githubRepo = config('dependencies.github_alias')[$user]."/{$package}";
273
        }
274
275
        // Use default package name
276
        else {
277
            $this->githubRepo = $fullPackageName;
278
        }
279
    }
280
281
    /**
282
     * Set the dependencies type.
283
     *
284
     * @param  string  $type
285
     */
286
    private function setType(string $type): void
287
    {
288
        assert(
289
            in_array($type, self::DEPENDENCY_TYPES),
290
            "'{$type} is not a supported dependency type (supported: ".join(', ', self::DEPENDENCY_TYPES)
291
        );
292
        $this->type = $type;
293
    }
294
295
    /**
296
     * Set the dependency project name.
297
     *
298
     * @param  string  $fullPackageName
299
     */
300
    private function setProject(string $fullPackageName): void
301
    {
302
        if ($this->type == 'python') {
303
            [$user, $package] = explode('/', $fullPackageName);
304
            $this->project = $package;
305
        } else {
306
            $this->project = $fullPackageName;
307
        }
308
    }
309
}
310