BaseTenantableCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 103
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 3
handleTenant() 0 1 ?
A parseTenantInput() 0 19 3
A getArguments() 0 4 1
A getOptions() 0 8 1
A fire() 0 4 1
A bootstrapTenant() 0 10 4
1
<?php
2
3
namespace TomSchlick\Townhouse;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\Console\Input\InputOption;
8
9
abstract class BaseTenantableCommand extends Command
10
{
11
    /**
12
     * Declare the handle method as final and use it to traverse through tenants.
13
     */
14
    final public function handle()
15
    {
16
        if (! $this->confirmToProceed()) {
0 ignored issues
show
Bug introduced by
The method confirmToProceed() does not exist on TomSchlick\Townhouse\BaseTenantableCommand. Did you maybe mean confirm()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
17
            return;
18
        }
19
20
        /** @var Tenant $tenant */
21
        foreach ($this->parseTenantInput() as $tenant) {
0 ignored issues
show
Bug introduced by
The expression $this->parseTenantInput() of type object<Illuminate\Support\Collection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
22
            $this->info("Starting for tenant: {$tenant->id} - {$tenant->name}");
23
24
            $this->bootstrapTenant($tenant);
25
26
            $this->handleTenant($tenant);
27
28
            $this->info("\nFinished for tenant: " . $tenant->id);
29
        }
30
    }
31
32
    /**
33
     * @param \TomSchlick\Townhouse\Tenant $tenant
34
     *
35
     * @return void
36
     */
37
    abstract public function handleTenant(Tenant $tenant) : void;
38
39
    /**
40
     * Get or ask for the tenant(s) you'd like to run the commands on.
41
     *
42
     * @return \Illuminate\Support\Collection|null
43
     */
44
    protected function parseTenantInput() : ?Collection
45
    {
46
        if ($this->option('all')) {
47
            return Tenant::all();
48
        }
49
50
        $input_ids = $this->option('tenant')
51
            ? $this->option('tenant')
52
            : $this->ask('Which tenant would you like to use? [tenant_id]');
53
54
        return Tenant::find(
55
            array_map(
56
                function ($id) {
57
                    return trim($id);
58
                },
59
                explode(',', $input_ids)
60
            )
61
        );
62
    }
63
64
    /**
65
     * Get the console command arguments.
66
     *
67
     * @return array
68
     */
69
    public function getArguments()
70
    {
71
        return [];
72
    }
73
74
    /**
75
     * Get the console command options.
76
     *
77
     * @return array
78
     */
79
    protected function getOptions()
80
    {
81
        return [
82
            ['tenant', null, InputOption::VALUE_REQUIRED, 'The tenant you want to run the command as.'],
83
            ['all', null, InputOption::VALUE_NONE, 'Run all tenants instead of just one.'],
84
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
85
        ];
86
    }
87
88
    /**
89
     * Override the fire method as well.
90
     */
91
    final public function fire()
92
    {
93
        $this->handle();
94
    }
95
96
    /**
97
     * Make Laravel ready for the tenant we want to run the command on.
98
     *
99
     * @param \TomSchlick\Townhouse\Tenant $tenant
100
     */
101
    protected function bootstrapTenant(Tenant $tenant)
102
    {
103
        if ($this->checkTenantDatabaseExists && ! $tenant->databaseExists()) {
0 ignored issues
show
Bug introduced by
The property checkTenantDatabaseExists does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
104
            $this->error('Tenant ' . $tenant->id . ' does not have a database setup.');
105
        }
106
107
        if ($tenant->databaseExists()) {
108
            $tenant->setCurrentTenantConfiguration();
109
        }
110
    }
111
}
112