CreateTenant   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B handle() 0 58 8
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Slides\Saml2\Helpers\ConsoleHelper;
6
use Slides\Saml2\Models\Tenant;
7
use Slides\Saml2\Repositories\TenantRepository;
8
9
/**
10
 * Class CreateTenant
11
 *
12
 * @package Slides\Saml2\Commands
13
 */
14
class CreateTenant extends \Illuminate\Console\Command
15
{
16
    use RendersTenants, ValidatesInput;
0 ignored issues
show
introduced by
The trait Slides\Saml2\Commands\RendersTenants requires some properties which are not provided by Slides\Saml2\Commands\CreateTenant: $uuid, $relay_state_url, $idp_x509_cert, $key, $idp_entity_id, $id, $name_id_format, $metadata, $created_at, $idp_logout_url, $idp_login_url, $updated_at, $deleted_at
Loading history...
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'saml2:create-tenant
24
                            { --k|key= : A tenant custom key }
25
                            { --entityId= : IdP Issuer URL }
26
                            { --loginUrl= : IdP Sign on URL }
27
                            { --logoutUrl= : IdP Logout URL }
28
                            { --relayStateUrl= : Redirection URL after successful login }
29
                            { --nameIdFormat= : Name ID Format ("persistent" by default) }
30
                            { --x509cert= : x509 certificate (base64) }
31
                            { --metadata= : A custom metadata }';
32
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Create a Tenant entity (relying identity provider)';
39
40
    /**
41
     * @var TenantRepository
42
     */
43
    protected $tenants;
44
45
    /**
46
     * DeleteTenant constructor.
47
     *
48
     * @param TenantRepository $tenants
49
     */
50
    public function __construct(TenantRepository $tenants)
51
    {
52
        $this->tenants = $tenants;
53
54
        parent::__construct();
55
    }
56
57
    /**
58
     * Execute the console command.
59
     *
60
     * @return void
61
     */
62
    public function handle()
63
    {
64
        if (!$entityId = $this->option('entityId')) {
65
            $this->error('Entity ID must be passed as an option --entityId');
66
            return;
67
        }
68
69
        if (!$loginUrl = $this->option('loginUrl')) {
70
            $this->error('Login URL must be passed as an option --loginUrl');
71
            return;
72
        }
73
74
        if (!$logoutUrl = $this->option('logoutUrl')) {
75
            $this->error('Logout URL must be passed as an option --logoutUrl');
76
            return;
77
        }
78
79
        if (!$x509cert = $this->option('x509cert')) {
80
            $this->error('x509 certificate (base64) must be passed as an option --x509cert');
81
            return;
82
        }
83
84
        $key = $this->option('key');
85
        $metadata = ConsoleHelper::stringToArray($this->option('metadata'));
86
87
        if($key && ($tenant = $this->tenants->findByKey($key))) {
88
            $this->renderTenants($tenant, 'Already found tenant(s) using this key');
89
            $this->error(
90
                'Cannot create a tenant because the key is already being associated with other tenants.'
91
                    . PHP_EOL . 'Firstly, delete tenant(s) or try to create with another with another key.'
92
            );
93
94
            return;
95
        }
96
97
        $class = config('saml2.tenantModel', Tenant::class);
0 ignored issues
show
Bug introduced by
The function config 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

97
        $class = /** @scrutinizer ignore-call */ config('saml2.tenantModel', Tenant::class);
Loading history...
98
        $tenant = new $class([
99
            'key' => $key,
100
            'uuid' => \Ramsey\Uuid\Uuid::uuid4(),
101
            'idp_entity_id' => $entityId,
102
            'idp_login_url' => $loginUrl,
103
            'idp_logout_url' => $logoutUrl,
104
            'idp_x509_cert' => $x509cert,
105
            'relay_state_url' => $this->option('relayStateUrl'),
106
            'name_id_format' => $this->resolveNameIdFormat(),
107
            'metadata' => $metadata,
108
        ]);
109
110
        if(!$tenant->save()) {
111
            $this->error('Tenant cannot be saved.');
112
            return;
113
        }
114
115
        $this->info("The tenant #{$tenant->id} ({$tenant->uuid}) was successfully created.");
116
117
        $this->renderTenantCredentials($tenant);
118
119
        $this->output->newLine();
120
    }
121
}