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

CollectionWriter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

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