sander3 /
laravel-gdpr
| 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
Loading history...
|
|||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | // Make the given attributes hidden |
||||||
| 27 | if (isset($this->gdprHidden)) { |
||||||
| 28 | $this->setHidden($this->gdprHidden); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 29 | } |
||||||
| 30 | |||||||
| 31 | return $this->toPortableArray(); |
||||||
| 32 | } |
||||||
| 33 | |||||||
| 34 | /** |
||||||
| 35 | * Eager load the given relations. |
||||||
| 36 | * |
||||||
| 37 | * @return void |
||||||
| 38 | */ |
||||||
| 39 | public function loadRelations(array $relations) |
||||||
| 40 | { |
||||||
| 41 | $portableRelations = $this->getPortableRelations($relations); |
||||||
| 42 | |||||||
| 43 | array_walk($portableRelations, [$this, 'loadPortableRelation']); |
||||||
| 44 | |||||||
| 45 | $this->load(array_diff($relations, $portableRelations)); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 46 | } |
||||||
| 47 | |||||||
| 48 | /** |
||||||
| 49 | * Get all portable relations. |
||||||
| 50 | * |
||||||
| 51 | * @return array |
||||||
| 52 | */ |
||||||
| 53 | private function getPortableRelations(array $relations) |
||||||
| 54 | { |
||||||
| 55 | $portableRelations = []; |
||||||
| 56 | |||||||
| 57 | foreach ($relations as $relation) { |
||||||
| 58 | if ($this->$relation()->getRelated() instanceof PortableContract) { |
||||||
| 59 | $portableRelations[] = $relation; |
||||||
| 60 | } |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | return $portableRelations; |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | /** |
||||||
| 67 | * Load and transform a portable relation. |
||||||
| 68 | * |
||||||
| 69 | * @return void |
||||||
| 70 | */ |
||||||
| 71 | private function loadPortableRelation(string $relation) |
||||||
| 72 | { |
||||||
| 73 | $instance = $this->$relation(); |
||||||
| 74 | |||||||
| 75 | $collection = $instance |
||||||
| 76 | ->get() |
||||||
| 77 | ->transform(function ($item) { |
||||||
| 78 | return $item->portable(); |
||||||
| 79 | }); |
||||||
| 80 | |||||||
| 81 | $class = class_basename(get_class($instance)); |
||||||
| 82 | |||||||
| 83 | if (in_array($class, ['HasOne', 'BelongsTo'])) { |
||||||
| 84 | $collection = $collection->first(); |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | $this->attributes[$relation] = $collection; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | /** |
||||||
| 91 | * Get the GDPR compliant data portability array for the model. |
||||||
| 92 | * |
||||||
| 93 | * @return array |
||||||
| 94 | */ |
||||||
| 95 | public function toPortableArray() |
||||||
| 96 | { |
||||||
| 97 | return $this->toArray(); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 98 | } |
||||||
| 99 | } |
||||||
| 100 |