Completed
Push — master ( b45acb...b74c11 )
by Vincent
30:46 queued 15:34
created

HasModel::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    public function setModel($model)
32 21
    {
33
        if ($model == null) {
34 21
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_NULL);
35 2
        }
36
        if (is_a($model, Model::class) === false) {
37
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_OBJECT);
38 19
        }
39
40 19
        $this->model = $model;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Get the model.
47
     *
48
     * @return Model
49
     */
50 2
    public function getModel()
51
    {
52 2
        return $this->model;
53 1
    }
54
55
    /**
56 1
     * Get the value of the model's primary key.
57
     *
58
     * @return mixed
59
     * @throws JsonApiFakerException
60
     */
61
    public function getKey()
62
    {
63
        if ($this->model == null) {
64
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_SET);
65
        }
66
67
        return $this->model->getKey();
68
    }
69
}
70