Completed
Push — master ( d94167...99c77e )
by Tom
03:03
created

BaseTenantableCommand::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace TomSchlick\Townhouse;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
8
abstract class BaseTenantableCommand extends Command
9
{
10
    /**
11
     * Declare the handle method as final and use it to traverse through tenants.
12
     */
13
    final public function handle()
14
    {
15
        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...
16
            return;
17
        }
18
19
        /** @var Tenant $tenant */
20
        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...
21
            $this->info("Starting for tenant: {$tenant->id} - {$tenant->name}");
22
23
            $this->bootstrapTenant($tenant);
24
25
            $this->handleTenant($tenant);
26
27
            $this->info("\nFinished for tenant: " . $tenant->id);
28
        }
29
    }
30
31
    /**
32
     * @param \TomSchlick\Townhouse\Tenant $tenant
33
     *
34
     * @return void
35
     */
36
    abstract public function handleTenant(Tenant $tenant) : void;
37
38
    /**
39
     * Get or ask for the tenant(s) you'd like to run the commands on.
40
     *
41
     * @return \Illuminate\Support\Collection|null
42
     */
43
    protected function parseTenantInput() : ?Collection
44
    {
45
        if ($this->option('all')) {
46
            return Tenant::all();
0 ignored issues
show
Bug introduced by
The method all() does not seem to exist on object<TomSchlick\Townhouse\Tenant>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        }
48
49
        $input_ids = $this->option('tenant')
50
            ? $this->option('tenant')
51
            : $this->ask('Which tenant would you like to use? [tenant_id]');
52
53
        return Tenant::find(
0 ignored issues
show
Bug introduced by
The method find() does not seem to exist on object<TomSchlick\Townhouse\Tenant>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            array_map(
55
                function ($id) {
56
                    return trim($id);
57
                },
58
                explode(',', $input_ids)
59
            )
60
        );
61
    }
62
63
    /**
64
     * Get the console command arguments.
65
     *
66
     * @return array
67
     */
68
    public function getArguments()
69
    {
70
        return [];
71
    }
72
73
    /**
74
     * Get the console command options.
75
     *
76
     * @return array
77
     */
78
    protected function getOptions()
79
    {
80
        return [
81
            ['tenant', null, InputOption::VALUE_REQUIRED, 'The tenant you want to run the command as.'],
82
            ['all', null, InputOption::VALUE_NONE, 'Run all tenants instead of just one.'],
83
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
84
        ];
85
    }
86
87
    /**
88
     * Override the fire method as well.
89
     */
90
    final public function fire()
91
    {
92
        $this->handle();
93
    }
94
95
    /**
96
     * Make Laravel ready for the tenant we want to run the command on.
97
     *
98
     * @param \TomSchlick\Townhouse\Tenant $tenant
99
     */
100
    protected function bootstrapTenant(Tenant $tenant)
101
    {
102
        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...
Bug introduced by
The method databaseExists() does not seem to exist on object<TomSchlick\Townhouse\Tenant>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            $this->error('Tenant ' . $tenant->id . ' does not have a database setup.');
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in TomSchlick\Townhouse\Tenant.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
104
        }
105
106
        if ($tenant->databaseExists()) {
0 ignored issues
show
Bug introduced by
The method databaseExists() does not seem to exist on object<TomSchlick\Townhouse\Tenant>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            $tenant->setCurrentTenantConfiguration();
0 ignored issues
show
Bug introduced by
The method setCurrentTenantConfiguration() does not seem to exist on object<TomSchlick\Townhouse\Tenant>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        }
109
    }
110
}
111