Completed
Push — master ( 883f28...097345 )
by Vincent
03:45 queued 11s
created

HasData   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 58
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setData() 0 5 1
A fakeData() 0 27 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
trait HasData
8
{
9
10
    /**
11
     * Undocumented variable
12
     *
13
     * @var ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null
14
     */
15
    public $data = null;
16
17
    /**
18
     * Undocumented function
19
     *
20
     * @param ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null $data
21
     * @return static
22
     */
23 14
    public function setData($data)
24
    {
25 14
        $this->data = $data;
26
27 14
        return $this;
28
    }
29
30
    /**
31
     * Undocumented function
32
     *
33
     * @param integer $options
34
     * @param integer $count
35
     *
36
     * @return static
37
     */
38 8
    public function fakeData($options = null, $count = null)
39
    {
40 8
        if (is_null($options)) {
41 1
            $options = self::FAKE_RESOURCE_OBJECT | self::FAKE_COLLECTION;
0 ignored issues
show
Bug introduced by
The constant VGirol\JsonApiFaker\Fact...a::FAKE_RESOURCE_OBJECT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant VGirol\JsonApiFaker\Fact...asData::FAKE_COLLECTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42 1
            $count = 5;
43
        }
44
45 8
        $faker = \Faker\Factory::create();
46
47 8
        if ($options & self::FAKE_CAN_BE_NULL) {
0 ignored issues
show
Bug introduced by
The constant VGirol\JsonApiFaker\Fact...sData::FAKE_CAN_BE_NULL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48 1
            if ($faker->boolean) {
49 1
                $this->setData(null);
50 1
                return $this;
51
            }
52
        }
53
54 8
        $class = (($options & self::FAKE_RESOURCE_IDENTIFIER) == self::FAKE_RESOURCE_IDENTIFIER) ?
0 ignored issues
show
Bug introduced by
The constant VGirol\JsonApiFaker\Fact...AKE_RESOURCE_IDENTIFIER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55 8
            ResourceIdentifierFactory::class : ResourceObjectFactory::class;
56
57 8
        if ($options & self::FAKE_COLLECTION) {
58 6
            $data = (new CollectionFactory)->fake($options, $count);
59
        } else {
60 2
            $data = (new $class)->fake();
61
        }
62 8
        $this->setData($data);
63
64 8
        return $this;
65
    }
66
}
67