Passed
Push — master ( 064793...c708b1 )
by Peter
02:48
created

GenerateKeys::doExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 21
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Console\Commands\Oauth2;
6
7
use Opulence\Console\Commands\Command;
8
use Opulence\Console\Responses\IResponse;
9
10
class GenerateKeys extends Command
11
{
12
    const NAME = 'oauth2:generatekeys';
13
14
    /** @var string */
15
    protected $privateKeyPassword;
16
17
    /** @var string */
18
    protected $privateKeyPath;
19
20
    /** @var string */
21
    protected $publicKeyPath;
22
23
    /**
24
     * GenerateKeys constructor.
25
     *
26
     * @param string $privateKeyPassword
27
     * @param string $privateKeyPath
28
     * @param string $publicKeyPath
29
     */
30
    public function __construct(string $privateKeyPassword, string $privateKeyPath, string $publicKeyPath)
31
    {
32
        $this->privateKeyPassword = $privateKeyPassword;
33
        $this->privateKeyPath     = $privateKeyPath;
34
        $this->publicKeyPath      = $publicKeyPath;
35
36
        parent::__construct();
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    protected function define()
43
    {
44
        $this->setName(static::NAME)
45
            ->setDescription('Generates openssl keys');
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    protected function doExecute(IResponse $response)
52
    {
53
        exec(
54
            sprintf(
55
                'openssl genrsa -passout pass:%s -out %s 2048',
56
                $this->privateKeyPassword,
57
                $this->privateKeyPath
58
            )
59
        );
60
61
        exec(
62
            sprintf(
63
                'openssl rsa -in %s -passin pass:%s -pubout -out %s',
64
                $this->privateKeyPath,
65
                $this->privateKeyPassword,
66
                $this->publicKeyPath
67
            )
68
        );
69
70
        chmod($this->privateKeyPath, 0600);
71
        chmod($this->publicKeyPath, 0600);
72
    }
73
}
74