HasModel   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 54
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 3 1
A getKey() 0 7 2
A setModel() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Laravel\Factory;
6
7
use Illuminate\Database\Eloquent\Model;
8
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
9
use VGirol\JsonApiFaker\Laravel\Messages;
10
11
/**
12
 * Add "model" member to a factory.
13
 */
14
trait HasModel
15
{
16
    /**
17
     * The model instance.
18
     *
19
     * @var Model
20
     */
21
    protected $model;
22
23
    /**
24
     * Set the model.
25
     *
26
     * @param Model $model
27
     *
28
     * @return static
29
     * @throws JsonApiFakerException
30
     */
31 66
    public function setModel($model)
32
    {
33 66
        if ($model == null) {
34 6
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_NULL);
35
        }
36 60
        if (is_a($model, Model::class) === false) {
37 3
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_OBJECT);
38
        }
39
40 57
        $this->model = $model;
41
42 57
        return $this;
43
    }
44
45
    /**
46
     * Get the model.
47
     *
48
     * @return Model
49
     */
50 27
    public function getModel()
51
    {
52 27
        return $this->model;
53
    }
54
55
    /**
56
     * Get the value of the model's primary key.
57
     *
58
     * @return mixed
59
     * @throws JsonApiFakerException
60
     */
61 6
    public function getKey()
62
    {
63 6
        if ($this->model == null) {
64 3
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_SET);
65
        }
66
67 3
        return $this->model->getKey();
68
    }
69
}
70