Completed
Push — master ( 872a00...e2f753 )
by Troy
01:33
created

CreateUser::createFakeUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.568
cc 3
nc 3
nop 0
1
<?php
2
3
namespace MultiTenantLaravel\App\Commands;
4
5
use Illuminate\Console\Command;
6
use Faker\Generator as Faker;
7
8
class CreateUser extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'tenant:create-user {--fake}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create a new user with the option to assign to tenants';
23
24
    protected $faker;
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct(Faker $faker)
32
    {
33
        $this->faker = $faker;
34
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $count = (int) $this->ask('How many would you like to create?');
46
47
        while ($count > 0) {
48
            if (!$this->option('fake')) {
49
                $this->createNewUser();
0 ignored issues
show
Unused Code introduced by
The call to the method MultiTenantLaravel\App\C...teUser::createNewUser() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
50
            } else {
51
                $this->createFakeUser();
52
            }
53
54
            $count--;
55
        }
56
57
    }
58
59
    private function createFakeUser()
60
    {
61
        $name = $this->faker->name;
62
63
        $user = config('multi-tenant.user_class')::create([
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
            'name' => $name,
65
            'email' => $this->faker->unique()->safeEmail,
66
            'password' => bcrypt('tester'),
67
        ]);
68
69
        $add_to_tenant = $this->anticipate('Would you like to assign the user to a tenant?', ['Yes', 'No'], 'Yes');
70
71
        if ($add_to_tenant == 'Yes') {
72
            $headers = ['Name', 'ID'];
73
            $tenants = config('multi-tenant.tenant_class')::all('name', 'id');
74
            if($tenants->count() <= 0) {
75
                $this->comment('No tenants available, bye');
76
            } else {
77
                $this->table($headers, $tenants->toArray());
78
            }
79
        }
80
    }
81
82
    private function createNewUser()
83
    {
84
        // Ask for some user input and create a new tenant
85
    }
86
}
87