Completed
Push — master ( da1a6c...6bb072 )
by Marco
02:08
created

GitPackage::printReason()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 0
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
    private function __construct(string $packageName, GitSignatureCheck ...$checks)
29
    {
30
        $this->packageName = $packageName;
31
        $this->checks      = $checks;
32
    }
33
34
    public static function fromPackageAndSignatureChecks(
35
        PackageInterface $package,
36
        GitSignatureCheck $firstCheck,
37
        GitSignatureCheck ...$checks
38
    ) : self {
39
        return new self($package->getName(), ...array_merge([$firstCheck], $checks));
40
    }
41
42
    public function packageName() : string
43
    {
44
        return $this->packageName;
45
    }
46
47
    public function isVerified() : bool
48
    {
49
        return (bool) $this->passedChecks();
50
    }
51
52
    public function printReason() : string
53
    {
54
        if ($this->isVerified()) {
55
            return sprintf('The following GIT GPG signature checks passed for package "%s":', $this->packageName())
56
                . "\n\n"
57
                . implode(
58
                    "\n\n",
59
                    array_map(
60
                        function (GitSignatureCheck $check) : string {
61
                            return $check->asHumanReadableString();
62
                        },
63
                        $this->passedChecks()
64
                    )
65
                );
66
        }
67
68
        return sprintf('The following GIT GPG signature checks have failed for package "%s":', $this->packageName())
69
            . "\n\n"
70
            . implode(
71
                "\n\n",
72
                array_map(
73
                    function (GitSignatureCheck $check) : string {
74
                        return $check->asHumanReadableString();
75
                    },
76
                    $this->failedChecks()
77
                )
78
            );
79
    }
80
81
    /**
82
     * @return GitSignatureCheck[]
83
     */
84
    private function passedChecks() : array
85
    {
86
        return array_values(array_filter(
87
            $this->checks,
88
            function (GitSignatureCheck $check) {
89
                return $check->canBeTrusted();
90
            }
91
        ));
92
    }
93
94
    /**
95
     * @return GitSignatureCheck[]
96
     */
97
    private function failedChecks() : array
98
    {
99
        return array_values(array_filter(
100
            $this->checks,
101
            function (GitSignatureCheck $check) {
102
                return ! $check->canBeTrusted();
103
            }
104
        ));
105
    }
106
}
107