1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\GrumphpLicenseTask\Service; |
6
|
|
|
|
7
|
|
|
use DateTimeZone; |
8
|
|
|
use Ergebnis\License\Holder; |
9
|
|
|
use Ergebnis\License\Range; |
10
|
|
|
use Ergebnis\License\Template; |
11
|
|
|
use Ergebnis\License\Year; |
12
|
|
|
use Exception; |
13
|
|
|
use GrumPHP\Util\Paths; |
14
|
|
|
use loophp\GrumphpLicenseTask\Entity\License; |
15
|
|
|
use loophp\GrumphpLicenseTask\Entity\LicenseInterface; |
16
|
|
|
|
17
|
|
|
use function in_array; |
18
|
|
|
|
19
|
|
|
use const PATHINFO_FILENAME; |
20
|
|
|
|
21
|
|
|
final class LicenseManager implements LicenseManagerInterface |
22
|
|
|
{ |
23
|
|
|
private Paths $paths; |
24
|
|
|
|
25
|
|
|
private SpdxLicensesInterface $spdxLicenses; |
26
|
|
|
|
27
|
6 |
|
public function __construct(SpdxLicensesInterface $spdxLicenses, Paths $paths) |
28
|
|
|
{ |
29
|
6 |
|
$this->paths = $paths; |
30
|
6 |
|
$this->spdxLicenses = $spdxLicenses; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
3 |
|
public function getAvailableLicenses(): array |
34
|
|
|
{ |
35
|
3 |
|
return array_map( |
36
|
3 |
|
static fn (string $filename): string => pathinfo($filename, PATHINFO_FILENAME), |
37
|
3 |
|
(array) glob(__DIR__ . '/../../resource/licenses/*.txt') |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function getLicenseFromFile(string $filepath, string $holder, int $fromYear): LicenseInterface |
42
|
|
|
{ |
43
|
1 |
|
return new License( |
44
|
1 |
|
Template::fromFile(sprintf('%s/%s', $this->paths->getProjectDir(), $filepath)), |
45
|
1 |
|
Range::since( |
46
|
1 |
|
Year::fromString((string) $fromYear), |
47
|
1 |
|
new DateTimeZone('UTC') |
48
|
|
|
), |
49
|
1 |
|
Holder::fromString($holder) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
public function getLicenseFromName(string $osiName, string $holder, ?int $fromYear): LicenseInterface |
54
|
|
|
{ |
55
|
3 |
|
if (false === $this->spdxLicenses->validate($osiName)) { |
56
|
1 |
|
throw new Exception('License is not a valid OSI license.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
if (false === in_array(strtolower($osiName), array_map('strtolower', $this->getAvailableLicenses()), true)) { |
60
|
1 |
|
throw new Exception( |
61
|
1 |
|
'License is valid but does not exist yet in loophp/grumphp-license-task. Submit a PR !' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$fromYear ??= date('Y'); |
66
|
|
|
|
67
|
1 |
|
return new License( |
68
|
1 |
|
Template::fromFile(sprintf('%s%s.txt', __DIR__ . '/../../resource/licenses/', $osiName)), |
69
|
1 |
|
Range::since( |
70
|
1 |
|
Year::fromString((string) $fromYear), |
71
|
1 |
|
new DateTimeZone('UTC') |
72
|
|
|
), |
73
|
1 |
|
Holder::fromString($holder) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|