DemoSeeder::seedMachines()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 28.10.18
6
 * Time: 16:15.
7
 */
8
9
namespace Modules\Demo\Database\Seeders;
10
11
use Illuminate\Database\Seeder;
12
use Modules\Account\Entities\Account;
13
use Modules\Auth0\Contracts\Auth0ServiceContract;
14
use Modules\Auth0\Services\Auth0Service;
15
use Modules\Auth0\Traits\Auth0TestUser;
16
use Modules\Authorization\Entities\Role;
17
use Modules\Machine\Entities\Machine;
18
use Modules\Proxy\Entities\Proxy;
19
20
class DemoSeeder extends Seeder
21
{
22
    use Auth0TestUser;
0 ignored issues
show
Bug introduced by
The trait Modules\Auth0\Traits\Auth0TestUser requires the property $id_token which is not provided by Modules\Demo\Database\Seeders\DemoSeeder.
Loading history...
23
24
    /**
25
     * @var bool
26
     */
27
    public $enabled = false;
28
29
    /**
30
     * @var Auth0Service
31
     */
32
    public $service;
33
34
    /**
35
     * DemoSeeder constructor.
36
     *
37
     * @param bool $seed
38
     */
39
    public function __construct(Auth0ServiceContract $auth0Service)
40
    {
41
        $this->service = $auth0Service;
0 ignored issues
show
Documentation Bug introduced by
$auth0Service is of type Modules\Auth0\Contracts\Auth0ServiceContract, but the property $service was declared to be of type Modules\Auth0\Services\Auth0Service. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
42
    }
43
44
    /**
45
     * Run the database seeds.
46
     *
47
     * @return void
48
     */
49
    public function run()
50
    {
51
        $user = $this->seedUser();
52
        $this->seedMachines($user);
53
        $this->seedProxies($user);
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    protected function seedUser()
60
    {
61
        $user = $this->getTestUser();
62
        $user->syncRoles(Role::ADMIN);
63
64
        return $user;
65
    }
66
67
    /**
68
     * @param $user
69
     * @return mixed
70
     */
71
    protected function seedMachines($user)
72
    {
73
        $machines = Machine::fromFactory(10)
74
            ->create([
75
                'user_id' => $user->id,
76
            ]);
77
78
        foreach ($machines as $machine) {
79
            $this->seedAccounts($machine);
80
        }
81
82
        return $machines;
83
    }
84
85
    protected function seedProxies($user)
86
    {
87
        $proxies = Proxy::fromFactory(30)
88
            ->create([
89
                'user_id' => $user->id,
90
            ]);
91
92
        foreach ($proxies as $proxy) {
93
            $this->seedAccounts($proxy);
94
        }
95
96
        return $proxy;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $proxy seems to be defined by a foreach iteration on line 92. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
97
    }
98
99
    /**
100
     * @param $machine
101
     * @return mixed
102
     */
103
    protected function seedAccounts($machine)
104
    {
105
        $accounts = Account::fromFactory(10)
106
            ->state('OSRS')
107
            ->create([
108
                'user_id' => $machine->user_id,
109
                'machine_id' => $machine->id,
110
            ]);
111
112
        return $accounts;
113
    }
114
}
115