Passed
Pull Request — master (#81)
by
unknown
06:45
created

CreateCertificate::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 19
rs 9.5222
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Illuminate\Console\Command;
6
7
/**
8
 * Class CreateCertificate
9
 *
10
 * @package Slides\Saml2\Commands
11
 */
12
class CreateCertificate extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'saml2:cert
20
                            {--days=7300 : Number of days to add from today as the expiration date}
21
                            {--keyname=key.pem : Full name of the certificate key file}
22
                            {--certname=cert.pem : Full name to the certificate file}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create new certificate and private key for your SP';
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return mixed
45
     */
46
    public function handle()
47
    {
48
        $storagePath = storage_path('samlsp');
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

48
        $storagePath = /** @scrutinizer ignore-call */ storage_path('samlsp');
Loading history...
49
        $days = $this->option('days');
50
        $keyname = $this->option('keyname');
51
        $certname = $this->option('certname');
52
53
        // Create storage/samlidp directory
54
        if (!file_exists($storagePath)) {
55
            mkdir($storagePath, 0755, true);
56
        }
57
58
        $key = sprintf('%s/%s', $storagePath, $keyname);
59
        $cert = sprintf('%s/%s', $storagePath, $certname);
60
        $question = 'The name chosen for the PEM files already exist. Would you like to overwrite existing PEM files?';
61
        if ((!file_exists($key) && !file_exists($cert)) || $this->confirm($question)) {
62
            $command = 'openssl req -x509 -sha256 -nodes -days %s -newkey rsa:2048 -keyout %s -out %s';
63
            exec(sprintf($command, $days, $key, $cert));
64
            $this->info("The certificate was successfully created.");
65
        }
66
    }
67
}