Completed
Push — master ( da0ab4...8ffee5 )
by Vincent
03:47
created

HasRelationships::getRelationships()   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
use VGirol\JsonApiFaker\Contract\RelationshipContract;
8
9
/**
10
 * Add "relationships" member to a factory.
11
 */
12
trait HasRelationships
13
{
14
    /**
15
     * The "relationships" member
16
     *
17
     * @var array An array of RelationshipFactory
18
     */
19
    protected $relationships;
20
21
    /**
22
     * Get the relationships collection
23
     *
24
     * @return array|null
25
     */
26 9
    public function getRelationships(): ?array
27
    {
28 9
        return $this->relationships;
29
    }
30
31
    /**
32
     * Add a single relationship
33
     *
34
     * @param string $name
35
     * @param RelationshipContract $relationship
36
     *
37
     * @return static
38
     */
39 27
    public function addRelationship(string $name, $relationship)
40
    {
41 27
        $this->addToObject('relationships', $name, $relationship);
0 ignored issues
show
Bug introduced by
It seems like addToObject() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

41
        $this->/** @scrutinizer ignore-call */ 
42
               addToObject('relationships', $name, $relationship);
Loading history...
42
43 27
        return $this;
44
    }
45
46
    /**
47
     * Fill the "relationships" member with fake values.
48
     *
49
     * @param integer $count The number of relationships to generate.
50
     *
51
     * @return static
52
     */
53 18
    public function fakeRelationships($count = 2)
54
    {
55 18
        $faker = \Faker\Factory::create();
56
57 18
        for ($i = 0; $i < $count; $i++) {
58 18
            $this->addRelationship(
59 18
                $faker->unique()->numerify('relationship##'),
60 18
                $this->generator->relationship()->fake()
61
            );
62
        }
63
64 18
        return $this;
65
    }
66
}
67