Passed
Push — master ( 9d6e1f...0a40f3 )
by Rutger
02:14
created

GPGHelper::sign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace rhertogh\Yii2SecurityTxt\helpers\GPG;
4
5
use Crypt_GPG;
6
use Crypt_GPG_Exception;
7
use PEAR_Exception;
8
use rhertogh\Yii2SecurityTxt\helpers\GPG\enums\GPGDriver;
9
use rhertogh\Yii2SecurityTxt\helpers\GPG\traits\CryptGPGTrait;
10
use rhertogh\Yii2SecurityTxt\helpers\GPG\traits\GnupgExtensionTrait;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * A helper class for the GNU Privacy Guard.
15
 * It can run with either the Crypt_GPG package or the "gnupg" extension.
16
 *
17
 * @link https://packagist.org/packages/pear/crypt_gpg
18
 * @link https://www.php.net/manual/en/book.gnupg.php
19
 */
20
class GPGHelper
21
{
22
    use CryptGPGTrait;
23
    use GnupgExtensionTrait;
24
25
    /** @var GPGDriver|null Specifies the library to use.
26
     * If `null` it will auto-detect which library to use in the following order: CryptGPG, GnupgExtension
27
     */
28
    public static GPGDriver|null $driver = null;
29
30
    /**
31
     * Sign a message.
32
     *
33
     * @throws InvalidConfigException
34
     * @throws Crypt_GPG_Exception
35
     * @throws PEAR_Exception
36
     */
37 2
    public static function sign(string $message, string $privateKey): string
38
    {
39 2
        return match (static::determineDriver()) {
40 2
            GPGDriver::CryptGPG => static::signViaCryptGPG($message, $privateKey),
41 2
            GPGDriver::GnupgExtension => static::signViaGnupgExtension($message, $privateKey),
42 2
        };
43
    }
44
45
    /**
46
     * @throws InvalidConfigException
47
     */
48 2
    protected static function determineDriver(): GPGDriver
49
    {
50 2
        $driver = static::$driver;
51
52 2
        if ($driver === null) {
53 1
            if (class_exists(Crypt_GPG::class)) {
54 1
                $driver = GPGDriver::CryptGPG;
55
            } elseif (extension_loaded('gnupg')) {
56
                $driver = GPGDriver::GnupgExtension;
57
            } else {
58 1
                throw new InvalidConfigException('Either the Crypt_GPG package (https://packagist.org/packages/pear/crypt_gpg)'
59 1
                    . ' or the "gnupg" extension (https://www.php.net/manual/en/book.gnupg.php) must be installed.');
60
            }
61
        } else {
62 2
            if ($driver === GPGDriver::CryptGPG) {
63 1
                if (!class_exists(Crypt_GPG::class)) {
64 1
                    throw new InvalidConfigException('When using the Crypt_GPG package (https://packagist.org/packages/pear/crypt_gpg) it must be installed.');
65
                }
66 1
            } elseif ($driver === GPGDriver::GnupgExtension) {
67 1
                if (!extension_loaded('gnupg')) {
68 1
                    throw new InvalidConfigException('When using the "gnupg" extension (https://www.php.net/manual/en/book.gnupg.php) it must be installed.');
69
                }
70
            } else {
71
                throw new \LogicException('Unknown GPGDriver "' . $driver->name . '".');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on rhertogh\Yii2SecurityTxt...ers\GPG\enums\GPGDriver.
Loading history...
72
            }
73
        }
74
75 2
        return $driver;
76
    }
77
}
78