Completed
Push — master ( 6dfed4...d83bff )
by Tom
02:38
created

src/Commands/BaseTenantableCommand.php (6 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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()) {
16
            return;
17
        }
18
19
        /** @var Tenant $tenant */
20
        foreach ($this->parseTenantInput() as $tenant) {
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
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
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
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
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
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
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