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

RendersTenants::renderTenants()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 20
nop 2
dl 0
loc 25
rs 8.8333
1
<?php
2
3
namespace Slides\Saml2\Commands;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class CreateTenant
9
 *
10
 * @package Slides\Saml2\Commands
11
 */
12
trait RendersTenants
13
{
14
    /**
15
     * Render tenants in a table.
16
     *
17
     * @param \Slides\Saml2\Models\Tenant|\Illuminate\Support\Collection $tenants
18
     * @param string|null $title
19
     *
20
     * @return void
21
     */
22
    protected function renderTenants($tenants, string $title = null)
23
    {
24
        /** @var \Slides\Saml2\Models\Tenant[]|\Illuminate\Database\Eloquent\Collection $tenants */
25
        $tenants = $tenants instanceof \Slides\Saml2\Models\Tenant
26
            ? collect([$tenants])
27
            : $tenants;
28
29
        $headers = ['Column', 'Value'];
30
        $columns = [];
31
32
        foreach ($tenants as $tenant) {
33
            foreach ($this->getTenantColumns($tenant) as $column => $value) {
34
                $columns[] = [$column, $value ?: '(empty)'];
35
            }
36
37
            if($tenants->last()->id !== $tenant->id) {
38
                $columns[] = new \Symfony\Component\Console\Helper\TableSeparator();
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\TableSeparator 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...
39
            }
40
        }
41
42
        if($title) {
43
            $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

43
            $this->/** @scrutinizer ignore-call */ 
44
                   getOutput()->title($title);
Loading history...
44
        }
45
46
        $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

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

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

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