Completed
Push — master ( 9e71bf...c98e96 )
by Sander
9s
created

Portable::getPortableRelations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Soved\Laravel\Gdpr;
4
5
use Soved\Laravel\Gdpr\Contracts\Portable as PortableContract;
6
7
trait Portable
8
{
9
    /**
10
     * Convert the model instance to a GDPR compliant data portability array.
11
     *
12
     * @return array
13
     */
14
    public function portable()
15
    {
16
        // Eager load the given relations
17
        if (isset($this->gdprWith)) {
18
            $this->loadRelations($this->gdprWith);
19
        }
20
21
        // Make the given attributes visible
22
        if (isset($this->gdprVisible)) {
23
            $this->setVisible($this->gdprVisible);
0 ignored issues
show
Bug introduced by
It seems like setVisible() 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

23
            $this->/** @scrutinizer ignore-call */ 
24
                   setVisible($this->gdprVisible);
Loading history...
24
        }
25
26
        // Make the given attributes hidden
27
        if (isset($this->gdprHidden)) {
28
            $this->setHidden($this->gdprHidden);
0 ignored issues
show
Bug introduced by
It seems like setHidden() 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

28
            $this->/** @scrutinizer ignore-call */ 
29
                   setHidden($this->gdprHidden);
Loading history...
29
        }
30
31
        return $this->toPortableArray();
32
    }
33
34
    /**
35
     * Eager load the given relations.
36
     *
37
     * @param  array  $relations
38
     * @return void
39
     */
40
    public function loadRelations(array $relations)
41
    {
42
        $portableRelations = $this->getPortableRelations($relations);
43
44
        array_walk($portableRelations, [$this, 'loadPortableRelation']);
45
46
        $this->load(array_diff($relations, $portableRelations));
0 ignored issues
show
Bug introduced by
It seems like load() 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

46
        $this->/** @scrutinizer ignore-call */ 
47
               load(array_diff($relations, $portableRelations));
Loading history...
47
    }
48
49
    /**
50
     * Get all portable relations.
51
     *
52
     * @param  array  $relations
53
     * @return array
54
     */
55
    private function getPortableRelations(array $relations)
56
    {
57
        $portableRelations = [];
58
59
        foreach ($relations as $relation) {
60
            if ($this->$relation()->getRelated() instanceof PortableContract) {
61
                $portableRelations[] = $relation;
62
            }
63
        }
64
65
        return $portableRelations;
66
    }
67
68
    /**
69
     * Load and transform a portable relation.
70
     *
71
     * @param  string  $relation
72
     * @return void
73
     */
74
    private function loadPortableRelation(string $relation)
75
    {
76
        $this->attributes[$relation] = $this
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
            ->$relation()
78
            ->get()
79
            ->transform(function ($item) {
80
                return $item->portable();
81
            });
82
    }
83
84
    /**
85
     * Get the GDPR compliant data portability array for the model.
86
     *
87
     * @return array
88
     */
89
    public function toPortableArray()
90
    {
91
        return $this->toArray();
0 ignored issues
show
Bug introduced by
It seems like toArray() 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

91
        return $this->/** @scrutinizer ignore-call */ toArray();
Loading history...
92
    }
93
}
94