Test Failed
Branch master (52949b)
by Artem
05:53
created

CreateTenant   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 55 8
A __construct() 0 5 1
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Slides\Saml2\Helpers\ConsoleHelper;
6
use Slides\Saml2\Repositories\TenantRepository;
7
8
/**
9
 * Class CreateTenant
10
 *
11
 * @package Slides\Saml2\Commands
12
 */
13
class CreateTenant extends \Illuminate\Console\Command
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
{
15
    use RendersTenants;
16
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'saml2:create-tenant
23
                            { --k|key= : A tenant custom key }
24
                            { --entityId= : IdP Issuer URL }
25
                            { --loginUrl= : IdP Sign on URL }
26
                            { --logoutUrl= : IdP Logout URL }
27
                            { --x509cert= : x509 certificate (base64) }
28
                            { --metadata= : A custom metadata }';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Create a Tenant entity (relying identity provider)';
36
37
    /**
38
     * @var TenantRepository
39
     */
40
    protected $tenants;
41
42
    /**
43
     * DeleteTenant constructor.
44
     *
45
     * @param TenantRepository $tenants
46
     */
47
    public function __construct(TenantRepository $tenants)
48
    {
49
        $this->tenants = $tenants;
50
51
        parent::__construct();
52
    }
53
54
    /**
55
     * Execute the console command.
56
     *
57
     * @return void
58
     */
59
    public function handle()
60
    {
61
        if (!$entityId = $this->option('entityId')) {
62
            $this->error('Entity ID must be passed as an option --entityId');
63
            return;
64
        }
65
66
        if (!$loginUrl = $this->option('loginUrl')) {
67
            $this->error('Login URL must be passed as an option --loginUrl');
68
            return;
69
        }
70
71
        if (!$logoutUrl = $this->option('logoutUrl')) {
72
            $this->error('Logout URL must be passed as an option --logoutUrl');
73
            return;
74
        }
75
76
        if (!$x509cert = $this->option('x509cert')) {
77
            $this->error('x509 certificate (base64) must be passed as an option --x509cert');
78
            return;
79
        }
80
81
        $key = $this->option('key');
82
        $metadata = ConsoleHelper::stringToArray($this->option('metadata'));
83
84
        if($key && ($tenant = $this->tenants->findByKey($key))) {
85
            $this->renderTenants($tenant, 'Already found tenant(s) using this key');
86
            $this->error(
87
                'Cannot create a tenant because the key is already being associated with other tenants.'
88
                    . PHP_EOL . 'Firstly, delete tenant(s) or try to create with another with another key.'
89
            );
90
91
            return;
92
        }
93
94
        $tenant = new \Slides\Saml2\Models\Tenant([
95
            'key' => $key,
96
            'uuid' => \Ramsey\Uuid\Uuid::uuid4(),
97
            'idp_entity_id' => $entityId,
98
            'idp_login_url' => $loginUrl,
99
            'idp_logout_url' => $logoutUrl,
100
            'idp_x509_cert' => $x509cert,
101
            'metadata' => $metadata,
102
        ]);
103
104
        if(!$tenant->save()) {
105
            $this->error('Tenant cannot be saved.');
106
            return;
107
        }
108
109
        $this->info("The tenant #{$tenant->id} ({$tenant->uuid}) was successfully created.");
110
111
        $this->renderTenantCredentials($tenant);
112
113
        $this->output->newLine();
114
    }
115
}