Passed
Push — master ( bc9222...b41766 )
by Rutger
12:36
created

GnupgExtensionTrait::signViaGnupgExtension()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 20
rs 9.7666
cc 3
nc 3
nop 2
1
<?php
2
3
namespace rhertogh\Yii2SecurityTxt\helpers\GPG\traits;
4
5
use gnupg;
6
use Yii;
7
use yii\base\InvalidConfigException;
8
9
trait GnupgExtensionTrait
10
{
11
    /**
12
     * @throws InvalidConfigException
13
     */
14
    protected static function signViaGnupgExtension($message, $privateKey)
15
    {
16
        Yii::beginProfile('Generate PGP signature', __METHOD__);
17
        $gpg = new gnupg();
18
        $gpg->seterrormode(GNUPG_ERROR_EXCEPTION);
19
20
        $keyInfo = $gpg->import($privateKey);
21
        if ($keyInfo === false) {
22
            throw new InvalidConfigException('Unable to import private key. Debug info: '
23
                . var_export(static::generateGnupgDebugInfo($gpg), true));
24
        }
25
        $gpg->addsignkey($keyInfo['fingerprint']);
26
        $gpg->setsignmode(gnupg::SIG_MODE_CLEAR);
0 ignored issues
show
Bug introduced by
The constant gnupg::SIG_MODE_CLEAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
        $output = $gpg->sign($message);
28
        if ($output === false) {
29
            throw new InvalidConfigException('Unable to sign the message. Debug info: '
30
                . var_export(static::generateGnupgDebugInfo($gpg), true));
31
        }
32
        Yii::endProfile('Generate PGP signature', __METHOD__);
33
        return $output;
34
    }
35
36
    protected static function generateGnupgDebugInfo(gnupg $gpg)
37
    {
38
        $debugInfo = [
39
            'engineInfo' => $gpg->getengineinfo(),
0 ignored issues
show
Bug introduced by
The method getengineinfo() does not exist on gnupg. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            'engineInfo' => $gpg->/** @scrutinizer ignore-call */ getengineinfo(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
            'errorInfo' => $gpg->geterrorinfo(),
0 ignored issues
show
Bug introduced by
The method geterrorinfo() does not exist on gnupg. Did you maybe mean geterror()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            'errorInfo' => $gpg->/** @scrutinizer ignore-call */ geterrorinfo(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
        ];
42
43
        if (empty($debugInfo['engineInfo']['home_dir'])) {
44
            $debugInfo['hint'] = 'The gnupg home directory is not set, it can be set via the GNUPGHOME environment variable.';
45
        } elseif (!is_writable($debugInfo['engineInfo']['home_dir'])) {
46
            $debugInfo['hint'] = 'The gnupg home directory (' . $debugInfo['engineInfo']['home_dir'] . ') might not be writable. '
47
                . 'Hint: can be changed via the GNUPGHOME environment variable.';
48
        }
49
50
        return $debugInfo;
51
    }
52
}
53