Test Failed
Push — master ( 259b7d...6518e1 )
by Gabriel
02:24
created

CryptHelper::loadKeysFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace ByTIC\Hello\Utility;
4
5
use League\OAuth2\Server\CryptKey;
6
use phpseclib\Crypt\RSA;
7
8
/**
9
 * Class Helper
10
 * @package ByTIC\Hello\Utility
11
 */
12
class CryptHelper
13
{
14
    /**
15
     * The storage location of the encryption keys.
16
     *
17
     * @var string
18
     */
19
    public static $keyPath;
20
21
    /**
22
     * Set the storage location of the encryption keys.
23
     *
24
     * @param string $path
25
     * @return void
26
     */
27
    public static function loadKeysFrom($path)
28
    {
29
        static::$keyPath = $path;
30
    }
31
32
    /**
33
     * The location of the encryption keys.
34
     *
35
     * @param string $file
36
     * @return string
37
     */
38 4
    public static function keyPath($file)
39
    {
40 4
        $file = ltrim($file, '/\\');
41 4
        if (static::$keyPath) {
42
            return rtrim(static::$keyPath, '/\\') . DIRECTORY_SEPARATOR . $file;
43
        }
44 4
        if (function_exists('Nip\storage_path')) {
45 4
            return \Nip\storage_path('hello/keys/' . $file);
46
        }
47
48
        if (function_exists('storage_path')) {
49
            return storage_path('hello/keys/' . $file);
50
        }
51
        return PathHelper::keys($file);
52
    }
53
54
    /**
55
     * @param null $basePath
56
     * @return bool
57
     */
58 2
    public static function generateKeys($basePath = null)
59
    {
60 2
        $privateKeyName = 'oauth-private.key';
61 2
        $publicKeyName = 'oauth-public.key';
62 2
        $privateKeyPath = $basePath ? $basePath . DIRECTORY_SEPARATOR . $privateKeyName : static::keyPath($privateKeyName);
63 2
        $publicKeyPath = $basePath ? $basePath . DIRECTORY_SEPARATOR . $publicKeyName : static::keyPath($publicKeyName);
64
65 2
        $rsa = new RSA();
66 2
        $keys = $rsa->createKey(2048, false);
67
68 2
        file_put_contents($privateKeyPath, $keys['privatekey']);
69 2
        file_put_contents($publicKeyPath, $keys['publickey']);
70
71 2
        $result = chmod($privateKeyPath, 0600);
72 2
        $result = $result && chmod($publicKeyPath, 0600);
73
74 2
        return $result;
75
    }
76
77
    /**
78
     * @param $type
79
     * @return CryptKey
80
     */
81 3
    public static function makeCryptKey($type)
82
    {
83 3
        $configKey = null;
0 ignored issues
show
Unused Code introduced by
$configKey is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84 3
        $configKey = ConfigHelper::get($type . '_key');
85
86 3
        $key = str_replace('\\n', "\n", $configKey);
87 3
        if (!$key) {
88 2
            $path = CryptHelper::keyPath('oauth-' . $type . '.key');
89 2
            if (!file_exists($path)) {
90 1
                CryptHelper::generateKeys(dirname($path));
91
            }
92 2
            $key = 'file://' . $path;
93
        }
94
95 3
        return new CryptKey($key, null, false);
96
    }
97
}
98