Completed
Pull Request — master (#443)
by
unknown
01:36
created

CollectionWriter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getCollection() 0 44 6
1
<?php
2
3
namespace Mpociot\ApiDoc\Postman;
4
5
use Ramsey\Uuid\Uuid;
6
use Illuminate\Support\Collection;
7
8
class CollectionWriter
9
{
10
    /**
11
     * @var Collection
12
     */
13
    private $routeGroups;
14
15
    /**
16
     * CollectionWriter constructor.
17
     *
18
     * @param Collection $routeGroups
19
     */
20
    public function __construct(Collection $routeGroups)
21
    {
22
        $this->routeGroups = $routeGroups;
23
    }
24
25
    public function getCollection()
26
    {
27
        $collection = [
28
            'variables' => [],
29
            'info' => [
30
                'name' => config('apidoc.postman.name') ?: config('app.name').' API',
31
                '_postman_id' => Uuid::uuid4()->toString(),
32
                'description' => config('apidoc.postman.description') ?: '',
33
                'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json',
34
            ],
35
            'item' => $this->routeGroups->map(function ($routes, $groupName) {
36
                return [
37
                    'name' => $groupName,
38
                    'description' => '',
39
                    'item' => $routes->map(function ($route) {
40
                        $mode = $route['methods'][0] === 'PUT' ? 'urlencoded' : 'formdata';
41
42
                        return [
43
                            'name' => $route['title'] != '' ? $route['title'] : url($route['uri']),
44
                            'request' => [
45
                                'url' => url($route['uri']),
46
                                'method' => $route['methods'][0],
47
                                'body' => [
48
                                    'mode' => $mode,
49
                                    $mode => collect($route['bodyParameters'])->map(function ($parameter, $key) {
50
                                        return [
51
                                            'key' => $key,
52
                                            'value' => isset($parameter['value']) ? $parameter['value'] : '',
53
                                            'type' => 'text',
54
                                            'enabled' => true,
55
                                        ];
56
                                    })->values()->toArray(),
57
                                ],
58
                                'description' => $route['description'],
59
                                'response' => [],
60
                            ],
61
                        ];
62
                    })->toArray(),
63
                ];
64
            })->values()->toArray(),
65
        ];
66
67
        return json_encode($collection);
68
    }
69
}
70