Completed
Push — 4-cactus ( 59c50f...21a27c )
by Alberto
02:40
created

BEdita/API/src/Controller/FoldersController.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace BEdita\API\Controller;
14
15
use BEdita\Core\Model\Action\ListRelatedFoldersAction;
16
use Cake\Network\Exception\NotFoundException;
17
use Cake\ORM\Association;
18
19
/**
20
 * Controller for `/folders` endpoint.
21
 *
22
 * The main aim is to bridge `parent` API relationship to `parents` BTM association.
23
 *
24
 * @since 4.0.0
25
 */
26
class FoldersController extends ObjectsController
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public $modelClass = 'Folders';
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected $_defaultConfig = [
37
        'allowedAssociations' => [
38
            'parent' => ['folders'],
39
            'children' => [],
40
        ],
41
    ];
42
43
    /**
44
     * {@inheritDoc}
45
     *
46
     * `parent` relationship is valid and will return `parents` association.
47
     * `parents` relationship is not allowed.
48
     */
49
    protected function findAssociation($relationship)
50
    {
51
        if ($relationship === 'parents') {
52
            throw new NotFoundException(__d('bedita', 'Relationship "{0}" does not exist', $relationship));
53
        }
54
55
        if ($relationship === 'parent') {
56
            return $this->Table->association('Parents');
57
        }
58
59
        return parent::findAssociation($relationship);
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    protected function getAvailableTypes($relationship)
66
    {
67
        if ($relationship === 'parent') {
68
            return ['folders'];
69
        }
70
        if ($relationship === 'children') {
71
            return ['objects'];
72
        }
73
74
        return parent::getAvailableTypes($relationship);
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     *
80
     * @return \BEdita\Core\Model\Action\ListRelatedFoldersAction
81
     */
82
    protected function getAssociatedAction(Association $association)
83
    {
84
        return new ListRelatedFoldersAction(compact('association'));
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     *
90
     * Folder with Parents association allows GET and PATCH
91
     */
92
    protected function setRelationshipsAllowedMethods(Association $association)
93
    {
94
        parent::setRelationshipsAllowedMethods($association);
95
96
        if ($association->getName() === 'Parents') {
97
            $allowedMethods = ['get', 'patch'];
98
            $this->request->allowMethod($allowedMethods);
0 ignored issues
show
The method allowMethod() does not exist on null. ( Ignorable by Annotation )

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

98
            $this->request->/** @scrutinizer ignore-call */ 
99
                            allowMethod($allowedMethods);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
        }
100
    }
101
}
102