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

UserNotificationFactory::createUserFactories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
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\User;
9
use Sfneal\Users\Models\UserNotification;
10
11
class UserNotificationFactory extends Factory
12
{
13
    public const TYPES = [
14
        'Domain\Clients\Notifications\NewInquiryNotification',
15
        'Domain\Plans\Notifications\BucketChangedNotification',
16
    ];
17
18
    /**
19
     * The name of the factory's corresponding model.
20
     *
21
     * @var string
22
     */
23
    protected $model = UserNotification::class;
24
25
    /**
26
     * UserNotificationFactory constructor.
27
     *
28
     * @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...
29
     * @param Collection|null $states
30
     * @param Collection|null $has
31
     * @param Collection|null $for
32
     * @param Collection|null $afterMaking
33
     * @param Collection|null $afterCreating
34
     * @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...
35
     */
36
    public function __construct($count = null, ?Collection $states = null, ?Collection $has = null, ?Collection $for = null, ?Collection $afterMaking = null, ?Collection $afterCreating = null, $connection = null)
37
    {
38
        parent::__construct($count, $states, $has, $for, $afterMaking, $afterCreating, $connection);
39
40
        self::createUserFactories();
41
    }
42
43
    /**
44
     * Define the model's default state.
45
     *
46
     * @return array
47
     */
48
    public function definition(): array
49
    {
50
        return [
51
            'user_id' => (new RandomModelAttributeQuery(User::class, 'id'))->execute(),
52
            'type' => $this->faker->randomElement(self::TYPES),
53
        ];
54
    }
55
56
    /**
57
     * Create User model factories if none exist.
58
     *
59
     * @return void
60
     */
61
    private static function createUserFactories(): void
62
    {
63
        if (! User::query()->count()) {
0 ignored issues
show
Bug introduced by
The method count() does not exist on Sfneal\Users\Builders\UserBuilder. 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

63
        if (! User::query()->/** @scrutinizer ignore-call */ count()) {
Loading history...
64
            User::factory()
65
                ->count(10)
66
                ->create();
67
        }
68
    }
69
}
70