Completed
Push — master ( 778409...c54dd1 )
by Vincent
03:02
created

HasData::dataHasBeenSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
/**
8
 * Add "data" member to a factory.
9
 */
10
trait HasData
11
{
12
13
    /**
14
     * The "data" member
15
     *
16
     * @var ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null
17
     */
18
    public $data;
19
20
    /**
21
     * Flag to indicate that "data" member has been set.
22
     * This can distinguish "data" member set to null to unset "data" member.
23
     *
24
     * @var boolean
25
     */
26
    private $hasBeenSet = false;
27
28
    /**
29
     * Set "data" member
30
     *
31
     * @param ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null $data
32
     * @return static
33
     */
34 15
    public function setData($data)
35
    {
36 15
        $this->data = $data;
37 15
        $this->hasBeenSet = true;
38
39 15
        return $this;
40
    }
41
42
    /**
43
     * Check if the "data" member has been set.
44
     *
45
     * @return boolean
46
     */
47 3
    public function dataHasBeenSet(): bool
48
    {
49 3
        return $this->hasBeenSet;
50
    }
51
52
    /**
53
     * Undocumented function
54
     *
55
     * @param integer $options Bitmask
56
     * @param integer $count In case of collection, it represents the number of resource object
57
     *                       or resource identifier to generate
58
     *
59
     * @return static
60
     */
61 9
    public function fakeData($options = null, $count = null)
62
    {
63 9
        if (is_null($options)) {
64 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...
65 1
            $count = 5;
66
        }
67
68 9
        $canBeNull = (($options & self::FAKE_CAN_BE_NULL) == 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...
69 9
        $isCollection = (($options & self::FAKE_COLLECTION) == self::FAKE_COLLECTION);
70 9
        $isRI = (($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...
71
72 9
        if ($canBeNull) {
73 1
            $faker = \Faker\Factory::create();
74
75 1
            if ($faker->boolean) {
76 1
                $this->setData(null);
77
78 1
                return $this;
79
            }
80
        }
81
82 9
        if ($isCollection) {
83 7
            $data = (new CollectionFactory)->fake($options, $count);
84
        } else {
85 3
            $data = $isRI ? (new ResourceIdentifierFactory)->fake() : (new ResourceObjectFactory)->fake();
86
        }
87 9
        $this->setData($data);
88
89 9
        return $this;
90
    }
91
}
92