Test Failed
Push — master ( 817d84...d52e3d )
by Raffael
05:44
created

Controller::_getNodes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Api\v2;
13
14
use Balloon\App\Api\v2\Collections as ApiCollection;
15
use Balloon\App\Api\v2\Files as ApiFile;
16
use Balloon\Filesystem;
17
use Balloon\Filesystem\Node\Collection;
18
use Balloon\Filesystem\Node\File;
19
use Balloon\Filesystem\Node\NodeInterface;
20
use Closure;
21
use Generator;
22
use Micro\Http\ExceptionInterface;
23
use Micro\Http\Response;
24
25
abstract class Controller
26
{
27
    /**
28
     * Filesystem.
29
     *
30
     * @var Filesystem
31
     */
32
    protected $fs;
33
34
    /**
35
     * Do bulk operations.
36
     */
37
    protected function bulk($id, Closure $action): Response
38
    {
39
        if (is_array($id)) {
40
            $body = [];
41
            foreach ($this->_getNodes($id) as $node) {
42
                try {
43
                    $body[(string) $node->getId()] = $action->call($this, $node);
44
                } catch (\Exception $e) {
45
                    $body[(string) $node->getId()] = [
46
                        'code' => $e instanceof ExceptionInterface ? $e->getStatusCode() : 400,
0 ignored issues
show
Bug introduced by
The class Micro\Http\ExceptionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
                        'data' => [
48
                            'error' => get_class($e),
49
                            'message' => $e->getMessage(),
50
                            'code' => $e->getCode(),
51
                        ],
52
                    ];
53
                }
54
            }
55
56
            return (new Response())->setCode(207)->setBody($body);
57
        }
58
59
        $body = $action->call($this, $this->_getNode($id));
60
        $response = (new Response())->setCode($body['code']);
61
62
        if (isset($body['data'])) {
63
            $response->setBody($body['data']);
64
        }
65
66
        return $response;
67
    }
68
69
    /**
70
     * Get node.
71
     *
72
     * @param string $id
73
     * @param string $class      Force set node type
74
     * @param bool   $multiple   Allow $id to be an array
75
     * @param bool   $allow_root Allow instance of root collection
76
     * @param bool   $deleted    How to handle deleted node
77
     */
78
    protected function _getNode(
79
        ?string $id = null,
80
        ?string $class = null,
81
        bool $multiple = false,
82
        bool $allow_root = false,
83
        int $deleted = 2
84
    ): NodeInterface {
85
        if (null === $class) {
86
            switch (get_class($this)) {
87
                case ApiFile::class:
88
                    $class = File::class;
89
90
                break;
91
                case ApiCollection::class:
92
                    $class = Collection::class;
93
94
                break;
95
            }
96
        }
97
98
        return $this->fs->getNode($id, $class, $multiple, $allow_root, $deleted);
99
    }
100
101
    /**
102
     * Get nodes.
103
     *
104
     * @param null|mixed $id
105
     */
106
    protected function _getNodes(
107
        $id = null,
108
        ?string $class = null,
109
        int $deleted = 2
110
    ): Generator {
111
        if (null === $class) {
112
            switch (get_class($this)) {
113
                case ApiFile::class:
114
                    $class = File::class;
115
116
                break;
117
                case ApiCollection::class:
118
                    $class = Collection::class;
119
120
                break;
121
            }
122
        }
123
124
        return $this->fs->getNodes($id, $class, $deleted);
125
    }
126
}
127