RiCollectionFactory::transform()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 2
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 identifier (@see ResourceIdentifierFactory).
12
 */
13
class RiCollectionFactory extends CollectionFactory
14
{
15
    /**
16
     * Returns an array of ResourceIdentifierContract.
17
     *
18
     * @param Collection $collection
19
     * @param string     $resourceType
20
     *
21
     * @return array
22
     */
23 21
    protected function transform($collection, $resourceType): array
24
    {
25 21
        return $collection->map(
26
            /**
27
             * @param \Illuminate\Database\Eloquent\Model $model
28
             *
29
             * @return \VGirol\JsonApiFaker\Laravel\Contract\ResourceIdentifierContract
30
             * @throws JsonApiFakerException
31
             */
32 21
            function ($model) use ($resourceType) {
33 21
                if (!is_a($model, Model::class)) {
34 3
                    throw new JsonApiFakerException(Messages::ERROR_NOT_MODEL_INSTANCE);
35
                }
36
37 18
                return $this->generator->resourceIdentifier($model, $resourceType);
38 21
            }
39 18
        )->toArray();
40
    }
41
}
42