RandomTrackable   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
dl 0
loc 46
rs 10
c 2
b 1
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A createTrackables() 0 6 1
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