Passed
Pull Request — master (#91)
by
unknown
05:05
created

UpdateTenant::handle()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 36
rs 8.9297
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 UpdateTenant
10
 *
11
 * @package Slides\Saml2\Commands
12
 */
13
class UpdateTenant extends \Illuminate\Console\Command
14
{
15
    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\UpdateTenant: $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...
16
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'saml2:update-tenant {id}
23
                            { --k|key= : A tenant custom key }
24
                            { --entityId= : IdP Issuer URL }
25
                            { --loginUrl= : IdP Sign on URL }
26
                            { --logoutUrl= : IdP Logout URL }
27
                            { --relayStateUrl= : Redirection URL after successful login }
28
                            { --nameIdFormat= : Name ID Format ("persistent" by default) }
29
                            { --x509cert= : x509 certificate (base64) }
30
                            { --metadata= : A custom metadata (JSON format) }';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Update a Tenant entity (relying identity provider)';
38
39
    /**
40
     * @var TenantRepository
41
     */
42
    protected $tenants;
43
44
    /**
45
     * DeleteTenant constructor.
46
     *
47
     * @param TenantRepository $tenants
48
     */
49
    public function __construct(TenantRepository $tenants)
50
    {
51
        $this->tenants = $tenants;
52
53
        parent::__construct();
54
    }
55
56
    /**
57
     * Execute the console command.
58
     *
59
     * @return void
60
     */
61
    public function handle()
62
    {
63
        if(!$tenant = $this->tenants->findById($this->argument('id'))) {
0 ignored issues
show
Bug introduced by
$this->argument('id') of type array|null|string is incompatible with the type integer expected by parameter $id of Slides\Saml2\Repositorie...tRepository::findById(). ( Ignorable by Annotation )

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

63
        if(!$tenant = $this->tenants->findById(/** @scrutinizer ignore-type */ $this->argument('id'))) {
Loading history...
64
            $this->error('Cannot find a tenant #' . $this->argument('id'));
0 ignored issues
show
Bug introduced by
Are you sure $this->argument('id') 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

64
            $this->error('Cannot find a tenant #' . /** @scrutinizer ignore-type */ $this->argument('id'));
Loading history...
65
            return;
66
        }
67
68
        if ($this->option('metadata')) {
69
            $metadata = $this->option('metadata') ? json_decode($this->option('metadata', '[]')) : null;
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Console\Command::option() has too many arguments starting with '[]'. ( Ignorable by Annotation )

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

69
            $metadata = $this->option('metadata') ? json_decode($this->/** @scrutinizer ignore-call */ option('metadata', '[]')) : null;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
            if (($json_error = json_last_error_msg()) !== 'No error') {
71
                $this->error($json_error);
72
                return;
73
            }
74
        }
75
76
        $tenant->update(array_filter([
77
            'key' => $this->option('key'),
78
            'idp_entity_id' => $this->option('entityId'),
79
            'idp_login_url' => $this->option('loginUrl'),
80
            'idp_logout_url' => $this->option('logoutUrl'),
81
            'idp_x509_cert' => $this->option('x509cert'),
82
            'relay_state_url' => $this->option('relayStateUrl'),
83
            'name_id_format' => $this->resolveNameIdFormat(),
84
            'metadata' => $metadata,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $metadata does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
        ]));
86
87
        if(!$tenant->save()) {
88
            $this->error('Tenant cannot be saved.');
89
            return;
90
        }
91
92
        $this->info("The tenant #{$tenant->id} ({$tenant->uuid}) was successfully updated.");
93
94
        $this->renderTenantCredentials($tenant);
95
96
        $this->output->newLine();
97
    }
98
}