Completed
Push — master ( c6a664...3473b8 )
by Alex
05:09
created

CollectionGenerator::getItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace AlexWells\ApiDocsGenerator\Postman;
4
5
use AlexWells\ApiDocsGenerator\Parsers\RouteWrapper;
6
use Ramsey\Uuid\Uuid;
7
use Illuminate\Support\Collection;
8
9
class CollectionGenerator
10
{
11
    /**
12
     * @var array[]
13
     */
14
    private $routes;
15
16
    /**
17
     * CollectionGenerator constructor.
18
     *
19
     * @param array[] $routes
20
     */
21
    public function __construct(array $routes)
22
    {
23
        $this->routes = $routes;
24
    }
25
26
    /**
27
     * Return generated collection in JSON format.
28
     *
29
     * All information is available here: https://schema.getpostman.com/json/collection/v2.0.0/docs/index.html
30
     *
31
     * @return string
32
     */
33
    public function getCollection()
34
    {
35
        $collection = array_merge([
36
            'info' => [
37
                'name' => $this->getName(),
38
                'description' => $this->getDescription(),
39
                'version' => $this->getVersion(),
40
                'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json',
41
            ]
42
        ], $this->getFolder());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getFolder() targeting AlexWells\ApiDocsGenerat...nGenerator::getFolder() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
44
        return json_encode($collection);
45
    }
46
47
    protected function getFolder($name = null) {
48
        $folder = [
0 ignored issues
show
Unused Code introduced by
The assignment to $folder is dead and can be removed.
Loading history...
49
            'name' => $name,
50
            'variable' => $this->getVariables($name),
51
            'event' => $this->getEvents($name),
52
            'auth' => $this->getAuth($name),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAuth($name) targeting AlexWells\ApiDocsGenerat...ionGenerator::getAuth() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
            'item' => $this->getItems($folder)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $folder seems to be never defined.
Loading history...
54
        ];
55
    }
56
57
    protected function getItems(array $input, array $formatted = []) {
58
        foreach ($input as $route) {
59
            $resource = $route['resource'];
60
61
            while (count($resource) > 1) {
62
                $key = array_shift($resource);
0 ignored issues
show
Unused Code introduced by
The assignment to $key is dead and can be removed.
Loading history...
63
64
                $folder = $this->getFolder($resource);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $folder is correct as $this->getFolder($resource) targeting AlexWells\ApiDocsGenerat...nGenerator::getFolder() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $folder is dead and can be removed.
Loading history...
65
            }
66
67
            $array[array_shift($resource)] = $value;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
68
69
            $formatted = array_merge($formatted, $array);
70
        }
71
72
        return [];
73
        /*$this->routeGroups->map(function ($routes, $groupName) {
74
            return [
75
                'name' => $groupName,
76
                'description' => '',
77
                'item' => $routes->map(function ($route) {
78
                    return [
79
                        'name' => $route['title'] != '' ? $route['title'] : url($route['uri']),
80
                        'request' => [
81
                            'url' => url($route['uri']),
82
                            'method' => $route['methods'][0],
83
                            'body' => [
84
                                'mode' => 'formdata',
85
                                'formdata' => collect($route['parameters']['query'])->map(function ($parameter) {
86
                                    return [
87
                                        'key' => $parameter['name'],
88
                                        'value' => '',
89
                                        'type' => collect($parameter['rules'])->contains(function ($rule) {
90
                                            return in_array($rule, ['file', 'image']);
91
                                            // TODO: advanced mime checks
92
                                        }) ? 'file' : 'text', // this check is fine for now
93
                                        'enabled' => true,
94
                                    ];
95
                                })->values()->toArray(),
96
                            ],
97
                            'description' => $route['description'],
98
                            'response' => [],
99
                        ],
100
                    ];
101
                })->toArray(),
102
            ];
103
        })->values()->toArray();*/
104
    }
105
106
    /**
107
     * Get display name
108
     *
109
     * @return string
110
     */
111
    protected function getName() {
112
        return config('app.name');
113
    }
114
115
    /**
116
     * Get description
117
     *
118
     * @return string|array
119
     */
120
    protected function getDescription() {
121
        return 'Automatically generated by laravel-api-docs-generator';
122
    }
123
124
    /**
125
     * Get version
126
     *
127
     * @return string
128
     */
129
    protected function getVersion() {
130
        return '1.0.0';
131
    }
132
133
    /**
134
     * Get set of variables
135
     *
136
     * @return array
137
     */
138
    protected function getVariables(string $folderName = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $folderName is not used and could be removed. ( Ignorable by Annotation )

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

138
    protected function getVariables(/** @scrutinizer ignore-unused */ string $folderName = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
        return [];
140
    }
141
142
    /**
143
     * Get set of events
144
     *
145
     * @return array
146
     */
147
    protected function getEvents(string $folderName = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $folderName is not used and could be removed. ( Ignorable by Annotation )

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

147
    protected function getEvents(/** @scrutinizer ignore-unused */ string $folderName = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
        return [];
149
    }
150
151
    /**
152
     * Get auth
153
     *
154
     * @return array|null
155
     */
156
    protected function getAuth(string $folderName = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $folderName is not used and could be removed. ( Ignorable by Annotation )

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

156
    protected function getAuth(/** @scrutinizer ignore-unused */ string $folderName = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
        return null;
158
    }
159
}
160