RandomTrackable::createTrackables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Sfneal\Tracking\Utils;
4
5
use Sfneal\Address\Models\Address;
6
use Sfneal\Queries\RandomModelAttributeQuery;
7
use Sfneal\Testing\Models\People;
8
9
class RandomTrackable
10
{
11
    /**
12
     * @var array Array of possible trackable types
13
     */
14
    private const MODELS = [People::class, Address::class];
15
16
    /**
17
     * The trackable's 'type'.
18
     *
19
     * @var People|Address|string
20
     */
21
    public $type;
22
23
    /**
24
     * * The trackable's 'id'.
25
     *
26
     * @var int
27
     */
28
    public $id;
29
30
    /**
31
     * RandomTrackable constructor.
32
     */
33
    public function __construct()
34
    {
35
        // Create People models if none exists
36
        if (! People::query()->count()) {
37
            self::createTrackables();
38
        }
39
40
        $this->type = self::MODELS[rand(0, count(self::MODELS) - 1)];
41
        $this->id = (new RandomModelAttributeQuery($this->type, $this->type::getPrimaryKeyName()))->execute();
42
    }
43
44
    /**
45
     * Create People models to attach to 'trackable' relationships.
46
     *
47
     * @return void
48
     */
49
    private static function createTrackables(): void
50
    {
51
        People::factory()
52
            ->count(20)
53
            ->has(Address::factory())
54
            ->create();
55
    }
56
}
57