Completed
Push — master ( c2388c...9bbc18 )
by Marcel
01:54
created

LaravelGenerator::callRoute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 2
eloc 16
nc 2
nop 7
1
<?php
2
3
namespace Mpociot\ApiDoc\Generators;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Support\Facades\Request;
8
9
class LaravelGenerator extends AbstractGenerator
10
{
11
    /**
12
     * @param Route $route
13
     *
14
     * @return mixed
15
     */
16
    protected function getUri($route)
17
    {
18
        return $route->getUri();
19
    }
20
21
    /**
22
     * @param  \Illuminate\Routing\Route $route
23
     * @param array $bindings
24
     * @param array $headers
25
     * @param bool $withResponse
26
     *
27
     * @return array
28
     */
29
    public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
30
    {
31
        $content = '';
32
33
        $routeAction = $route->getAction();
34
        $routeGroup = $this->getRouteGroup($routeAction['uses']);
35
        $routeDescription = $this->getRouteDescription($routeAction['uses']);
36
37
38
        if ($withResponse) {
39
            $response = $this->getRouteResponse($route, $bindings, $headers);
40
            if ($response->headers->get('Content-Type') === 'application/json') {
41
                $content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT);
42
            } else {
43
                $content = $response->getContent();
44
            }
45
        }
46
47
        return $this->getParameters([
48
            'id' => md5($route->getUri().':'.implode($route->getMethods())),
49
            'resource' => $routeGroup,
50
            'title' => $routeDescription['short'],
51
            'description' => $routeDescription['long'],
52
            'methods' => $route->getMethods(),
53
            'uri' => $route->getUri(),
54
            'parameters' => [],
55
            'response' => $content,
56
        ], $routeAction, $bindings);
57
    }
58
59
    /**
60
     * Call the given URI and return the Response.
61
     *
62
     * @param  string  $method
63
     * @param  string  $uri
64
     * @param  array  $parameters
65
     * @param  array  $cookies
66
     * @param  array  $files
67
     * @param  array  $server
68
     * @param  string  $content
69
     *
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
73
    {
74
        $kernel = App::make('Illuminate\Contracts\Http\Kernel');
75
        App::instance('middleware.disable', true);
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\App. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
77
        $server = collect([
78
            'CONTENT_TYPE' => 'application/json',
79
            'Accept' => 'application/json',
80
        ])->merge($server)->toArray();
81
82
        $request = Request::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean createFreshMockInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
83
            $uri, $method, $parameters,
84
            $cookies, $files, $this->transformHeadersToServerVars($server), $content
85
        );
86
87
        $response = $kernel->handle($request);
88
89
        $kernel->terminate($request, $response);
90
91
        if (file_exists($file = App::bootstrapPath().'/app.php')) {
92
            $app = require $file;
93
            $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
94
        }
95
96
        return $response;
97
    }
98
}
99