RoCollectionFactory::appendRelationships()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace VGirol\JsonApiFaker\Laravel\Factory;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
8
use VGirol\JsonApiFaker\Laravel\Messages;
9
10
/**
11
 * Factory for collection of resource object (@see ResourceObjectFactory).
12
 */
13
class RoCollectionFactory extends CollectionFactory
14
{
15
    /**
16
     * Add a relationship to the resource object.
17
     *
18
     * @param array $relationships
19
     *
20
     * @return static
21
     * @throws JsonApiFakerException
22
     */
23 6
    public function appendRelationships(array $relationships)
24
    {
25 6
        return $this->each(
26
            /**
27
             * @param \VGirol\JsonApiFaker\Laravel\Contract\ResourceObjectContract $resFactory
28
             *
29
             * @return void
30
             */
31 6
            function ($resFactory) use ($relationships) {
32 3
                $resFactory->appendRelationships($relationships);
33 6
            }
34
        );
35
    }
36
37
    /**
38
     * Returns a collection of ResourceObjectContract.
39
     *
40
     * @param Collection $collection
41
     * @param string     $resourceType
42
     *
43
     * @return array
44
     */
45 9
    protected function transform($collection, $resourceType): array
46
    {
47 9
        return $collection->map(
48
            /**
49
             * @param Model $model
50
             *
51
             * @return \VGirol\JsonApiFaker\Laravel\Contract\ResourceObjectContract
52
             * @throws JsonApiFakerException
53
             */
54 9
            function ($model) use ($resourceType) {
55 9
                if (!is_a($model, Model::class)) {
56 3
                    throw new JsonApiFakerException(Messages::ERROR_NOT_MODEL_INSTANCE);
57
                }
58
59 6
                return $this->generator->resourceObject($model, $resourceType);
60 9
            }
61 6
        )->toArray();
62
    }
63
}
64