Test Failed
Pull Request — master (#88)
by Artem
04:05
created

Update::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Cerbero\CommandValidator\ValidatesInput;
6
use Illuminate\Validation\Rules\Exists;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\Rules\Exists 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...
7
use Illuminate\Validation\Rules\In;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\Rules\In 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...
8
use Illuminate\Validation\Rules\Unique;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\Rules\Unique 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...
9
use Slides\Saml2\Helpers\ConsoleHelper;
10
use Slides\Saml2\Repositories\IdentityProviderRepository;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class Update 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\Update: $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:idp-update {id}
24
                            { --key : Key/name of the Identity Provider }
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 = 'Update an existing Identity Provider';
39
40
    /**
41
     * @var IdentityProviderRepository
42
     */
43
    protected IdentityProviderRepository $repository;
44
45
    /**
46
     * DeleteTenant constructor.
47
     *
48
     * @param IdentityProviderRepository $repository
49
     */
50
    public function __construct(IdentityProviderRepository $repository)
51
    {
52
        $this->repository = $repository;
53
54
        parent::__construct();
55
    }
56
57
    /**
58
     * Execute the console command.
59
     *
60
     * @return void
61
     */
62
    public function handle()
63
    {
64
        $tenant = $this->repository->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...rRepository::findById(). ( Ignorable by Annotation )

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

64
        $tenant = $this->repository->findById(/** @scrutinizer ignore-type */ $this->argument('id'));
Loading history...
65
66
        $tenant->update(array_filter([
67
            'key' => $this->option('key'),
68
            'idp_entity_id' => $this->option('entityId'),
69
            'idp_login_url' => $this->option('loginUrl'),
70
            'idp_logout_url' => $this->option('logoutUrl'),
71
            'idp_x509_cert' => $this->option('x509cert'),
72
            'relay_state_url' => $this->option('relayStateUrl'),
73
            'name_id_format' => $this->option('nameIdFormat'),
74
            'metadata' => ConsoleHelper::stringToArray($this->option('metadata'))
75
        ]));
76
77
        if (!$tenant->save()) {
78
            $this->error('Identity Provider cannot be saved.');
79
            return;
80
        }
81
82
        $this->info("The tenant #$tenant->id ($tenant->uuid) was successfully updated.");
83
84
        $this->renderTenantCredentials($tenant);
0 ignored issues
show
Bug introduced by
It seems like $tenant can also be of type Illuminate\Database\Eloquent\Model and null; however, parameter $idp of Slides\Saml2\Commands\Up...nderTenantCredentials() does only seem to accept Slides\Saml2\Contracts\IdentityProvidable, 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

84
        $this->renderTenantCredentials(/** @scrutinizer ignore-type */ $tenant);
Loading history...
85
86
        $this->output->newLine();
87
    }
88
89
    /**
90
     * @param InputInterface $input
91
     * @param OutputInterface $output
92
     *
93
     * @return void
94
     */
95
    protected function interact(InputInterface $input, OutputInterface $output)
96
    {
97
        if (! $input->getOption('entityId')) {
98
            $input->setOption('entityId', $this->ask('Entity ID (fx. https://sts.windows.net/65b9e948-757b-4431-b140-62a2f8a3fdeb/) (optional)'));
99
        }
100
101
        if (! $input->getOption('loginUrl')) {
102
            $input->setOption('loginUrl', $this->ask('Login URL (fx. https://login.microsoftonline.com/65b9e948-757b-4431-b140-62a2f8a3fdeb/saml2)'));
103
        }
104
105
        if (! $input->getOption('logoutUrl')) {
106
            $input->setOption('logoutUrl', $this->ask('Logout URL'));
107
        }
108
109
        if (! $input->getOption('nameIdFormat')) {
110
            $input->setOption('nameIdFormat', $this->choice('Name ID Format', $this->nameIdFormatValues(), 'persistent'));
111
        }
112
113
        if (! $input->getOption('relayStateUrl')) {
114
            $input->setOption('relayStateUrl', $this->ask('Post-login redirect URL (optional)'));
115
        }
116
117
        if (! $input->getOption('key')) {
118
            $input->setOption('key', $this->ask('Key/name of the identity provider (optional)'));
119
        }
120
121
        if (! $input->getOption('metadata')) {
122
            $input->setOption('metadata', $this->ask('Custom metadata (in format "field:value,anotherfield:value") (optional)'));
123
        }
124
    }
125
126
    /**
127
     * Validation rules for the user input.
128
     *
129
     * @return array
130
     */
131
    protected function rules(): array
132
    {
133
        return [
134
            'id' => ['required', 'int', new Exists(config('saml2.idpModel'))],
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

134
            'id' => ['required', 'int', new Exists(/** @scrutinizer ignore-call */ config('saml2.idpModel'))],
Loading history...
135
            'key' => ['string', new Unique(config('saml2.idpModel'), 'key')],
136
            'entityId' => 'string',
137
            'loginUrl' => 'string|url',
138
            'logoutUrl' => 'string|url',
139
            'x509cert' => 'string',
140
            'relayStateUrl' => 'string|url',
141
            'metadata' => 'string',
142
            'nameIdFormat' => ['string', new In($this->nameIdFormatValues())]
143
        ];
144
    }
145
146
    /**
147
     * Get the values for the name ID format rule.
148
     *
149
     * @return string[]
150
     */
151
    protected function nameIdFormatValues(): array
152
    {
153
        return [
154
            'persistent',
155
            'transient',
156
            'emailAddress',
157
            'unspecified',
158
            'X509SubjectName',
159
            'WindowsDomainQualifiedName',
160
            'kerberos',
161
            'entity'
162
        ];
163
    }
164
}
165