DummyCommand::handle()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace AtlassianConnectCore\Console;
4
5
use AtlassianConnectCore\Models\Tenant;
6
use AtlassianConnectCore\Services\TenantService;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Validation\Rule;
10
11
/**
12
 * Class DummyCommand
13
 *
14
 * @package AtlassianConnectCore\Console
15
 */
16
class DummyCommand extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'plugin:dummy';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Make your tenant dummy';
31
32
    /**
33
     * @var TenantService
34
     */
35
    protected $tenantService;
36
37
    /**
38
     * InstallCommand constructor.
39
     *
40
     * @param TenantService $tenantService
41
     */
42
    public function __construct(TenantService $tenantService)
43
    {
44
        parent::__construct();
45
46
        $this->tenantService = $tenantService;
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return mixed
53
     */
54
    public function handle()
55
    {
56
        $tenants = $this->tenantService->findReals();
57
58
        $this->table(
59
            ['ID', 'Client Key (last 22 chars)', 'Product', 'Status', 'Created At'],
60
            $this->formatTenants($tenants, [
0 ignored issues
show
Bug introduced by
$tenants of type Illuminate\Database\Eloquent\Collection is incompatible with the type AtlassianConnectCore\Mod...ase\Eloquent\Collection expected by parameter $tenants of AtlassianConnectCore\Con...ommand::formatTenants(). ( Ignorable by Annotation )

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

60
            $this->formatTenants(/** @scrutinizer ignore-type */ $tenants, [
Loading history...
61
                'id', 'client_key', 'product_type', 'event_type', 'created_at'
62
            ])
63
        );
64
65
        // Make the list of choices
66
        $choices = $tenants
67
            ->pluck('id')
68
            ->toArray();
69
70
        $id = $this->ask('Which the tenant should be dummy? (Pass ID)');
71
72
        $validator = Validator::make(['value' => $id], ['value' => Rule::in($choices)]);
73
74
        if($validator->fails()) {
75
            return $this->error('Invalid ID provided');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->error('Invalid ID provided') targeting Illuminate\Console\Command::error() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
        }
77
78
        $this->tenantService->makeDummy($id);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of AtlassianConnectCore\Ser...antService::makeDummy(). ( Ignorable by Annotation )

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

78
        $this->tenantService->makeDummy(/** @scrutinizer ignore-type */ $id);
Loading history...
79
80
        $this->comment('Tenant <info>' . $id . '</info> has been set as dummy');
81
    }
82
83
    /**
84
     * Format given tenants to columns with specific attributes
85
     *
86
     * @param \Illuminate\Database\Eloquent\Collection|Tenant[] $tenants
87
     * @param array $attributes Attributes need to be returned
88
     *
89
     * @return array
90
     */
91
    protected function formatTenants($tenants, array $attributes)
92
    {
93
        $tenants = collect($tenants->toArray());
94
95
        return $tenants->map(function ($tenant) use ($attributes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tenants->map(function(...) { /* ... */ }) returns the type Illuminate\Support\Collection which is incompatible with the documented return type array.
Loading history...
96
97
            // Make the client key shorter for displaying
98
            $tenant['client_key'] = substr($tenant['client_key'], -22);
99
100
            return array_only($tenant, $attributes);
101
        });
102
    }
103
}