RendersTenants   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTenantColumns() 0 16 3
B renderTenants() 0 25 7
A renderArray() 0 9 2
A renderTenantCredentials() 0 10 2
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Slides\Saml2\Models\Tenant;
6
use Illuminate\Support\Str;
7
8
/**
9
 * Class CreateTenant
10
 *
11
 * @package Slides\Saml2\Commands
12
 */
13
trait RendersTenants
14
{
15
    /**
16
     * Render tenants in a table.
17
     *
18
     * @param \Slides\Saml2\Models\Tenant|\Illuminate\Support\Collection $tenants
19
     * @param string|null $title
20
     *
21
     * @return void
22
     */
23
    protected function renderTenants($tenants, ?string $title = null)
24
    {
25
        /** @var \Slides\Saml2\Models\Tenant[]|\Illuminate\Database\Eloquent\Collection $tenants */
26
        $tenants = $tenants instanceof Tenant
27
            ? collect([$tenants])
28
            : $tenants;
29
30
        $headers = ['Column', 'Value'];
31
        $columns = [];
32
33
        foreach ($tenants as $tenant) {
34
            foreach ($this->getTenantColumns($tenant) as $column => $value) {
35
                $columns[] = [$column, $value ?: '(empty)'];
36
            }
37
38
            if($tenants->last()->id !== $tenant->id) {
39
                $columns[] = new \Symfony\Component\Console\Helper\TableSeparator();
40
            }
41
        }
42
43
        if($title) {
44
            $this->getOutput()->title($title);
0 ignored issues
show
Bug introduced by
It seems like getOutput() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

44
            $this->/** @scrutinizer ignore-call */ 
45
                   getOutput()->title($title);
Loading history...
45
        }
46
47
        $this->table($headers, $columns);
0 ignored issues
show
Bug introduced by
It seems like table() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
        $this->/** @scrutinizer ignore-call */ 
48
               table($headers, $columns);
Loading history...
48
    }
49
50
    /**
51
     * Get a columns of the Tenant.
52
     *
53
     * @param \Slides\Saml2\Models\Tenant $tenant
54
     *
55
     * @return array
56
     */
57
    protected function getTenantColumns(Tenant $tenant)
58
    {
59
        return [
60
            'ID' => $tenant->id,
61
            'UUID' => $tenant->uuid,
62
            'Key' => $tenant->key,
63
            'Entity ID' => $tenant->idp_entity_id,
64
            'Login URL' => $tenant->idp_login_url,
65
            'Logout URL' => $tenant->idp_logout_url,
66
            'Relay State URL' => $tenant->relay_state_url,
67
            'Name ID format' => $tenant->name_id_format,
68
            'x509 cert' => Str::limit($tenant->idp_x509_cert, 50),
69
            'Metadata' => $this->renderArray($tenant->metadata ?: []),
70
            'Created' => $tenant->created_at->toDateTimeString(),
71
            'Updated' => $tenant->updated_at->toDateTimeString(),
72
            'Deleted' => $tenant->deleted_at ? $tenant->deleted_at->toDateTimeString() : null
73
        ];
74
    }
75
76
    /**
77
     * Render a tenant credentials.
78
     *
79
     * @param \Slides\Saml2\Models\Tenant $tenant
80
     *
81
     * @return void
82
     */
83
    protected function renderTenantCredentials(Tenant $tenant)
84
    {
85
        $this->output->section('Credentials for the tenant');
86
87
        $this->getOutput()->text([
88
            'Identifier (Entity ID): <comment>' . route('saml.metadata', ['uuid' => $tenant->uuid]) . '</comment>',
0 ignored issues
show
Bug introduced by
The function route 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

88
            'Identifier (Entity ID): <comment>' . /** @scrutinizer ignore-call */ route('saml.metadata', ['uuid' => $tenant->uuid]) . '</comment>',
Loading history...
89
            'Reply URL (Assertion Consumer Service URL): <comment>' . route('saml.acs', ['uuid' => $tenant->uuid]) . '</comment>',
90
            'Sign on URL: <comment>' . route('saml.login', ['uuid' => $tenant->uuid]) . '</comment>',
91
            'Logout URL: <comment>' . route('saml.logout', ['uuid' => $tenant->uuid]) . '</comment>',
92
            'Relay State: <comment>' . ($tenant->relay_state_url ?: config('saml2.loginRoute')) . ' (optional)</comment>'
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

92
            'Relay State: <comment>' . ($tenant->relay_state_url ?: /** @scrutinizer ignore-call */ config('saml2.loginRoute')) . ' (optional)</comment>'
Loading history...
93
        ]);
94
    }
95
96
    /**
97
     * Print an array to a string.
98
     *
99
     * @param array $array
100
     *
101
     * @return string
102
     */
103
    protected function renderArray(array $array)
104
    {
105
        $lines = [];
106
107
        foreach ($array as $key => $value) {
108
            $lines[] = "$key: $value";
109
        }
110
111
        return implode(PHP_EOL, $lines);
112
    }
113
}