DatabaseFaker   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 6
dl 0
loc 99
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 22 2
A getArguments() 0 4 1
A getOptions() 0 4 1
A seedUsers() 0 20 4
A __construct() 0 6 1
B seedCategories() 0 26 3
1
<?php namespace App\Console\Commands;
2
3
use App\Models\Categories;
4
use App\Models\Groups;
5
use App\Models\Products;
6
use App\User;
7
use Artisan;
8
use DB;
9
use Faker\Factory;
10
use Hash;
11
use Illuminate\Console\Command;
12
13
class DatabaseFaker extends Command {
14
15
    const NB_USERS = 20;
16
    const MAX_NB_PRODUCTS = 9;
17
18
	protected $name = 'db:faker';
19
	protected $description = 'Seed the database with fake data for testing purposes';
20
21
    private $faker;
22
23
	public function __construct()
24
	{
25
		parent::__construct();
26
27
        $this->faker = Factory::create();
28
	}
29
30
	public function fire()
31
	{
32
        $this->error("\nWatch out!");
33
        $this->info('This command will truncate all your Barmate data.');
34
        $this->info("Use this command only for testing purposes.\n");
35
36
        if( $this->confirm('Are you sure you want to continue? ', false)==false )
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->confirm('Are you ... to continue? ', false) of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
37
        {
38
            $this->info('Command aborted');
39
            return 1;
40
        }
41
42
        Artisan::call('db:seed', ['--force'=>true]);
43
        $this->info('Created administrator account \'[email protected]\' with password \'password\'.');
44
45
        $groupId = Groups::firstOrFail()->group_id;
46
47
        $this->seedUsers($groupId);
48
        $this->seedCategories($groupId);
49
50
        $this->info("\nFake data successfully created. Happy testing!");
51
	}
52
53
	protected function getArguments()
54
	{
55
		return [];
56
	}
57
58
	protected function getOptions()
59
	{
60
		return [];
61
	}
62
63
    private function seedUsers($groupId)
64
    {
65
        for($i=0; $i<self::NB_USERS; $i++)
66
        {
67
            $firstName = $this->faker->firstName;
68
            $lastName = $this->faker->lastName;
69
            $email = strtolower($firstName.'.'.$lastName.'@'.$this->faker->domainName);
70
71
            User::create([  'firstname'         => $firstName,
72
                            'lastname'          => $lastName,
73
                            'group_id'          => $groupId,
74
                            'email'             => $email,
75
                            'password_hash'     => Hash::make('password'),
76
                            'role'              => ( $i%2 == 0 ) ? 'USER' : 'MNGR',
77
                            'inscription_date'  => $this->faker->dateTime,
78
                            'is_active'         => ( $i%5 == 0 ) ? false : true ]);
79
        }
80
81
        $this->info('Added '.self::NB_USERS.' users to the application with password \'password\'.');
82
    }
83
84
    private function seedCategories($groupId)
85
    {
86
        DB::table('categories')->delete();
87
88
        $categories = ['Saison', 'Gueuze', 'Tripel', 'Witbier', 'Dubbel'];
89
        shuffle($categories);
90
91
        foreach($categories as $categoryName)
92
        {
93
            $category = Categories::create(['group_id'          => $groupId,
94
                                            'category_title'    => $categoryName]);
95
96
            $nbProducts = rand(1, self::MAX_NB_PRODUCTS);
97
98
            for($i=1; $i<=$nbProducts; $i++)
99
            {
100
                Products::create(['category_id' => $category->category_id,
0 ignored issues
show
Documentation introduced by
The property category_id does not exist on object<App\Models\Categories>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
                                    'product_name' => $categoryName.''.$i,
102
                                    'description' => '',
103
                                    'price' => rand(1, 4),
104
                                    'quantity' => rand(24,48)]);
105
            }
106
        }
107
108
        $this->info('Created '.count($categories).' categories with products in the application.');
109
    }
110
111
}
112