ResourceObjectFactory::fillRelationship()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace VGirol\JsonApiFaker\Laravel\Factory;
4
5
use Illuminate\Database\Eloquent\Relations\Relation;
6
use VGirol\JsonApiFaker\Contract\RelationshipContract;
7
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
8
use VGirol\JsonApiFaker\Factory\ResourceObjectFactory as BaseFactory;
9
use VGirol\JsonApiFaker\Laravel\Contract\ResourceObjectContract;
10
use VGirol\JsonApiFaker\Laravel\Messages;
11
12
/**
13
 * A factory for resource object.
14
 */
15
class ResourceObjectFactory extends BaseFactory implements ResourceObjectContract
16
{
17 1
    use IsResource {
18
        setValues as setValuesTrait;
19
    }
20
21
    /**
22
     * @throws JsonApiFakerException
23
     */
24 24
    public function setValues($model, string $resourceType)
25
    {
26 24
        $this->setValuesTrait($model, $resourceType);
27
28 24
        if ($model != null) {
29 24
            $this->setAttributes($model->attributesToArray());
30
        }
31
32 24
        return $this;
33
    }
34
35
    /**
36
     * Add relationship factories.
37
     *
38
     * @param array $relationships
39
     *
40
     * @return static
41
     */
42 6
    public function appendRelationships(array $relationships)
43
    {
44 6
        foreach ($relationships as $name => $resourceType) {
45 6
            $this->loadRelationship($name, $resourceType);
46
        }
47
48 6
        return $this;
49
    }
50
51
    /**
52
     * Add a relationship factory.
53
     *
54
     * @param string $name
55
     * @param string $resourceType
56
     *
57
     * @return static
58
     * @throws JsonApiFakerException
59
     */
60 15
    public function loadRelationship(string $name, string $resourceType)
61
    {
62 15
        $relationship = $this->createRelationshipFactory();
63
64 15
        return $this->fillRelationship($relationship, $name, $resourceType)
65 12
            ->addRelationship($name, $relationship);
66
    }
67
68
    /**
69
     * Fill a relationship factory.
70
     *
71
     * Creates a \Illuminate\Database\Eloquent\Relations\Relation instance
72
     * and fill the relationshipFactory with it.
73
     *
74
     * @param RelationshipFactory $relationship
75
     * @param string              $name
76
     * @param string              $resourceType
77
     *
78
     * @return static
79
     * @throws JsonApiFakerException
80
     */
81 15
    protected function fillRelationship($relationship, string $name, string $resourceType)
82
    {
83 15
        $relation = $this->getRelationObject($name);
84 12
        $relationship->setData($relation, $resourceType);
85
86 12
        return $this;
87
    }
88
89
    /**
90
     * Create an instance of RelationshipFactory.
91
     *
92
     * @param mixed ...$args
93
     *
94
     * @return RelationshipContract
95
     */
96 15
    protected function createRelationshipFactory(...$args)
97
    {
98 15
        return $this->generator
99 15
            ->relationship(...$args);
100
    }
101
102
    /**
103
     * Return the object's relationship.
104
     *
105
     * @param string $name
106
     *
107
     * @return Relation
108
     * @throws JsonApiFakerException
109
     */
110 15
    private function getRelationObject(string $name)
111
    {
112 15
        if ($this->model === null) {
113 3
            throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_SET);
114
        }
115
116 12
        if (!$this->model->relationLoaded($name)) {
117 3
            $this->model->load($name);
118
        }
119
120 12
        return $this->model->getRelation($name);
121
    }
122
}
123