GitPackage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 91
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromPackageAndSignatureChecks() 0 7 1
A packageName() 0 4 1
A isVerified() 0 4 1
B printReason() 0 28 2
A passedChecks() 0 9 1
A failedChecks() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ComposerGpgVerify\Package;
6
7
use Composer\Package\PackageInterface;
8
use Roave\ComposerGpgVerify\Package\Git\GitSignatureCheck;
9
10
/**
11
 * @internal do not use: I will cut you.
12
 *
13
 * This class represents a package verification performed via GIT+GPG verifications. It really is just a
14
 * collection of @see GitSignatureCheck
15
 */
16
final class GitPackage implements PackageVerification
17
{
18
    /**
19
     * @var string
20
     */
21
    private $packageName;
22
23
    /**
24
     * @var GitSignatureCheck[]
25
     */
26
    private $checks;
27
28 5
    private function __construct(string $packageName, GitSignatureCheck ...$checks)
29
    {
30 5
        $this->packageName = $packageName;
31 5
        $this->checks      = $checks;
32 5
    }
33
34 5
    public static function fromPackageAndSignatureChecks(
35
        PackageInterface $package,
36
        GitSignatureCheck $firstCheck,
37
        GitSignatureCheck ...$checks
38
    ) : self {
39 5
        return new self($package->getName(), ...array_merge([$firstCheck], $checks));
40
    }
41
42 5
    public function packageName() : string
43
    {
44 5
        return $this->packageName;
45
    }
46
47 5
    public function isVerified() : bool
48
    {
49 5
        return (bool) $this->passedChecks();
50
    }
51
52 5
    public function printReason() : string
53
    {
54 5
        if ($this->isVerified()) {
55 3
            return sprintf('The following GIT GPG signature checks passed for package "%s":', $this->packageName())
56 3
                . "\n\n"
57 3
                . implode(
58 3
                    "\n\n",
59 3
                    array_map(
60
                        function (GitSignatureCheck $check) : string {
61 3
                            return $check->asHumanReadableString();
62 3
                        },
63 3
                        $this->passedChecks()
64
                    )
65
                );
66
        }
67
68 2
        return sprintf('The following GIT GPG signature checks have failed for package "%s":', $this->packageName())
69 2
            . "\n\n"
70 2
            . implode(
71 2
                "\n\n",
72 2
                array_map(
73
                    function (GitSignatureCheck $check) : string {
74 2
                        return $check->asHumanReadableString();
75 2
                    },
76 2
                    $this->failedChecks()
77
                )
78
            );
79
    }
80
81
    /**
82
     * @return GitSignatureCheck[]
83
     */
84 5
    private function passedChecks() : array
85
    {
86 5
        return array_values(array_filter(
87 5
            $this->checks,
88
            function (GitSignatureCheck $check) {
89 5
                return $check->canBeTrusted();
90 5
            }
91
        ));
92
    }
93
94
    /**
95
     * @return GitSignatureCheck[]
96
     */
97 2
    private function failedChecks() : array
98
    {
99 2
        return array_values(array_filter(
100 2
            $this->checks,
101 2
            function (GitSignatureCheck $check) {
102 2
                return ! $check->canBeTrusted();
103 2
            }
104
        ));
105
    }
106
}
107