Passed
Pull Request — master (#17)
by Stephen
14:26
created

UserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 7
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Database\Factories;
4
5
use Illuminate\Database\Eloquent\Factories\Factory;
6
use Illuminate\Support\Collection;
7
use Sfneal\Queries\RandomModelAttributeQuery;
8
use Sfneal\Users\Models\Role;
9
use Sfneal\Users\Models\User;
10
11
class UserFactory extends Factory
12
{
13
    /**
14
     * The name of the factory's corresponding model.
15
     *
16
     * @var string
17
     */
18
    protected $model = User::class;
19
20
    /**
21
     * UserFactory constructor.
22
     *
23
     * @param null $count
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $count is correct as it would always require null to be passed?
Loading history...
24
     * @param Collection|null $states
25
     * @param Collection|null $has
26
     * @param Collection|null $for
27
     * @param Collection|null $afterMaking
28
     * @param Collection|null $afterCreating
29
     * @param null $connection
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $connection is correct as it would always require null to be passed?
Loading history...
30
     */
31
    public function __construct($count = null, ?Collection $states = null, ?Collection $has = null, ?Collection $for = null, ?Collection $afterMaking = null, ?Collection $afterCreating = null, $connection = null)
32
    {
33
        parent::__construct($count, $states, $has, $for, $afterMaking, $afterCreating, $connection);
34
35
        // Create Role models if none exist
36
        self::createRoleFactories();
37
    }
38
39
    /**
40
     * Define the model's default state.
41
     *
42
     * @return array
43
     */
44
    public function definition(): array
45
    {
46
        return [
47
            'role_id' => (new RandomModelAttributeQuery(Role::class, 'role_id'))->execute(),
48
            'first_name' => $this->faker->firstName,
49
            'middle_name' => $this->faker->randomElement([$this->faker->randomLetter, null]),
50
            'last_name' => $this->faker->lastName,
51
            'nickname' => $this->faker->randomElement([$this->faker->firstName]),
52
            'nickname_preferred' => $this->faker->numberBetween(0, 1),
53
            'title' => $this->faker->jobTitle,
54
            'suffix' => null,
55
            'email' => $this->faker->email,
56
            'rate' => $this->faker->numberBetween(20, 200),
57
            'phone_work' => $this->faker->phoneNumber,
58
            'phone_mobile' => $this->faker->phoneNumber,
59
            'fax' => $this->faker->phoneNumber,
60
            'website' => $this->faker->url,
61
            'bio' => $this->faker->text(255),
62
            'username' => $this->faker->userName,
63
            'password' => $this->faker->password,
64
            'status' => $this->faker->numberBetween(0, 1),
65
            'remember_token' => null,
66
        ];
67
    }
68
69
    /**
70
     * Create Role model factories if none exist.
71
     *
72
     * @return void
73
     */
74
    private static function createRoleFactories(): void
75
    {
76
        if (! Role::query()->count()) {
0 ignored issues
show
Bug introduced by
The method count() does not exist on Sfneal\Users\Builders\RoleBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        if (! Role::query()->/** @scrutinizer ignore-call */ count()) {
Loading history...
77
            Role::factory()
78
                ->count(10)
79
                ->create();
80
        }
81
    }
82
}
83