Passed
Push — master ( 34beb8...79bf97 )
by Alberto
14:24 queued 13s
created

ChildrenComponent::removeRelated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * BEdita, API-first content management framework
5
 * Copyright 2023 Atlas Srl, Chialab Srl
6
 *
7
 * This file is part of BEdita: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
13
 */
14
15
namespace App\Controller\Component;
16
17
use App\Utility\ApiClientTrait;
18
use Cake\Controller\Component;
19
use Cake\Utility\Hash;
20
21
/**
22
 * Children component.
23
 * This component is used to add/remove children to a folder.
24
 * When a child is a subfolder, this use `PATCH /folders/:id/relationships/parent` to set parent to null.
25
 * When a child is not a subfolder, it's removed as usual by `removeRelated`.
26
 */
27
class ChildrenComponent extends Component
28
{
29
    use ApiClientTrait;
30
31
    /**
32
     * Add children to a folder.
33
     *
34
     * @param string $parentId Folder ID.
35
     * @param array $children Children objects as id/type pairs.
36
     * @return array
37
     */
38
    public function addRelated(string $parentId, array $children): array
39
    {
40
        $results = [];
41
        foreach ($children as $child) {
42
            $results[] = $this->addRelatedChild($parentId, $child);
43
        }
44
45
        return $results;
46
    }
47
48
    /**
49
     * Add single child by parent ID and child data.
50
     *
51
     * @param string $parentId The parent ID.
52
     * @param array $child The child data (id, type, meta).
53
     * @return array|null
54
     */
55
    public function addRelatedChild(string $parentId, array $child): ?array
56
    {
57
        $type = (string)Hash::get($child, 'type');
58
        if ($type !== 'folders') {
59
            return $this->getClient()->addRelated($parentId, 'folders', 'children', [$child]);
60
        }
61
62
        return $this->getClient()->addRelated($parentId, 'folders', 'parent', $child);
63
    }
64
65
    /**
66
     * Remove folder children.
67
     * When a child is a subfolder, this use `PATCH /folders/:id/relationships/parent` to set parent to null
68
     * When a child is not a subfolder, it's removed as usual by `removeRelated`
69
     *
70
     * @param string $parentId Folder ID
71
     * @param array $children Children objects as id/type pairs.
72
     * @return array
73
     */
74
    public function removeRelated(string $parentId, array $children): array
75
    {
76
        $results = [];
77
        foreach ($children as $child) {
78
            $results[] = $this->removeRelatedChild($parentId, $child);
79
        }
80
81
        return $results;
82
    }
83
84
    /**
85
     * Remove single child by parent ID and child data.
86
     *
87
     * @param string $parentId The parent ID.
88
     * @param array $child The child data (id, type, meta).
89
     * @return array|null
90
     */
91
    public function removeRelatedChild(string $parentId, array $child): ?array
92
    {
93
        $type = (string)Hash::get($child, 'type');
94
        if ($type === 'folders') {
95
            // invert relation call => use 'parent' relation on children folder
96
            return $this->getClient()->replaceRelated((string)Hash::get($child, 'id'), 'folders', 'parent', [null]);
97
        }
98
99
        return $this->getClient()->removeRelated($parentId, 'folders', 'children', [$child]);
100
    }
101
}
102