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

CollectionFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 77
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIlluminateCollection() 0 3 1
B setCollection() 0 42 7
1
<?php
2
3
namespace VGirol\JsonApiFaker\Laravel\Factory;
4
5
use Illuminate\Support\Collection;
6
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
7
use VGirol\JsonApiFaker\Factory\CollectionFactory as BaseFactory;
8
use VGirol\JsonApiFaker\Laravel\Contract\CollectionContract;
9
use VGirol\JsonApiFaker\Laravel\Contract\ResourceIdentifierContract;
10
use VGirol\JsonApiFaker\Laravel\Contract\ResourceObjectContract;
11
use VGirol\JsonApiFaker\Laravel\Messages;
12
13
/**
14
 * Factory for collection of resource object (@see ResourceObjectFactory)
15
 * or resource identifier (@see ResourceIdentifierFactory).
16
 */
17
abstract class CollectionFactory extends BaseFactory implements CollectionContract
18
{
19
    /**
20
     * A collection of models.
21
     *
22
     * @var Collection|null
23
     */
24
    protected $collection;
25
26
    public function getIlluminateCollection()
27
    {
28
        return $this->collection;
29
    }
30
31
    /**
32
     * Set the collection.
33 15
     *
34
     * @param Collection|array|null $provided     An array of objects implementing ResourceObjectContract
35 15
     *                                            or ResourceIdentifierContract
36 15
     * @param string|null           $resourceType
37
     *
38 15
     * @return static
39 11
     * @throws JsonApiFakerException
40 1
     */
41
    public function setCollection($provided, $resourceType = null)
42
    {
43 10
        $collection = null;
44 10
        $array = null;
45
46
        if (is_a($provided, Collection::class)) {
0 ignored issues
show
Bug introduced by
It seems like $provided can also be of type array; however, parameter $object of is_a() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        if (is_a(/** @scrutinizer ignore-type */ $provided, Collection::class)) {
Loading history...
47 12
            if ($resourceType === null) {
48 4
                throw new JsonApiFakerException(Messages::ERROR_TYPE_NOT_NULL);
49 4
            }
50
51
            $collection = $provided;
52
            $array = $this->transform($collection, $resourceType);
53
        }
54
55
        if (is_array($provided)) {
56
            $array = $provided;
57
            $collection = collect($provided)->map(
58 3
                /**
59 1
                 * @param ResourceObjectContract|ResourceIdentifierContract $item
60
                 *
61 2
                 * @return \Illuminate\Database\Eloquent\Model
62 1
                 * @throws JsonApiFakerException
63
                 */
64
                function ($item) {
65 1
                    $isValid = is_a($item, ResourceObjectContract::class)
66 4
                        || is_a($item, ResourceIdentifierContract::class);
67
                    if (!$isValid) {
68
                        throw new JsonApiFakerException(Messages::ERROR_NOT_FACTORY_INSTANCE);
69
                    }
70 10
                    if ($item->getModel() == null) {
71 10
                        throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_SET);
72
                    }
73 10
74
                    return $item->getModel();
75
                }
76
            );
77
        }
78
79
        $this->collection = $collection;
80
        parent::setCollection($array);
81
82
        return $this;
83
    }
84
85
    /**
86
     * Returns a collection of resource identifier or resource object factories.
87
     *
88
     * @param Collection $collection
89
     * @param string     $resourceType
90
     *
91
     * @return array<ResourceObjectContract>|array<ResourceIdentifierContract>
92
     */
93
    abstract protected function transform($collection, $resourceType): array;
94
}
95