Passed
Push — master ( f529b1...8cf222 )
by Stephen
03:09 queued 12s
created

DependencyService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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