DeleteTenant   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 36 5
A __construct() 0 5 1
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Slides\Saml2\Repositories\TenantRepository;
6
7
/**
8
 * Class DeleteTenant
9
 *
10
 * @package Slides\Saml2\Commands
11
 */
12
class DeleteTenant extends \Illuminate\Console\Command
13
{
14
    use RendersTenants;
0 ignored issues
show
introduced by
The trait Slides\Saml2\Commands\RendersTenants requires some properties which are not provided by Slides\Saml2\Commands\DeleteTenant: $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...
15
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'saml2:delete-tenant {tenant}
22
                            { --safe : Safe deletion }';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Delete a tenant by ID, key or UUID';
30
31
    /**
32
     * @var TenantRepository
33
     */
34
    protected $tenants;
35
36
    /**
37
     * DeleteTenant constructor.
38
     *
39
     * @param TenantRepository $tenants
40
     */
41
    public function __construct(TenantRepository $tenants)
42
    {
43
        $this->tenants = $tenants;
44
45
        parent::__construct();
46
    }
47
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return void
52
     */
53
    public function handle()
54
    {
55
        $tenants = $this->tenants->findByAnyIdentifier($this->argument('tenant'), false);
0 ignored issues
show
Bug introduced by
It seems like $this->argument('tenant') can also be of type array; however, parameter $key of Slides\Saml2\Repositorie...::findByAnyIdentifier() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $tenants = $this->tenants->findByAnyIdentifier(/** @scrutinizer ignore-type */ $this->argument('tenant'), false);
Loading history...
56
57
        if($tenants->isEmpty()) {
58
            $this->error('Cannot find a matching tenant by "' . $this->argument('tenant') . '" identifier');
0 ignored issues
show
Bug introduced by
Are you sure $this->argument('tenant') of type array|null|string can be used in concatenation? ( Ignorable by Annotation )

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

58
            $this->error('Cannot find a matching tenant by "' . /** @scrutinizer ignore-type */ $this->argument('tenant') . '" identifier');
Loading history...
59
            return;
60
        }
61
62
        $this->renderTenants($tenants, 'Found tenant(s)');
63
64
        if($tenants->count() > 1) {
65
            $deletingId = $this->ask('We have found several tenants, which one would you like to delete? (enter its ID)');
66
        }
67
        else {
68
            $deletingId = $tenants->first()->id;
69
        }
70
71
        $tenant = $tenants->firstWhere('id', $deletingId);
72
73
        if($this->option('safe')) {
74
            $tenant->delete();
75
76
            $this->info('The tenant #' . $deletingId . ' safely deleted. To restore it, run:');
77
            $this->output->block('php artisan saml2:restore-tenant ' . $deletingId);
78
79
            return;
80
        }
81
82
        if(!$this->confirm('Would you like to forcely delete the tenant #' . $deletingId . '? It cannot be reverted.')) {
83
            return;
84
        }
85
86
        $tenant->forceDelete();
87
88
        $this->info('The tenant #' . $deletingId . ' safely deleted.');
89
    }
90
}