Passed
Pull Request — master (#154)
by Alex
07:05
created

ODataController::index()   D

Complexity

Conditions 10
Paths 654

Size

Total Lines 70
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 70
ccs 0
cts 19
cp 0
rs 4.1666
cc 10
eloc 52
nc 654
nop 1
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Controllers;
4
5
use AlgoWeb\PODataLaravel\Controllers\Controller as BaseController;
6
use AlgoWeb\PODataLaravel\Serialisers\IronicSerialiser;
7
use Carbon\Carbon;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\Response;
10
use Illuminate\Support\Facades\App;
11
use Illuminate\Support\Facades\DB;
12
use Illuminate\Support\Facades\Storage;
13
use POData\OperationContext\ServiceHost as ServiceHost;
14
use POData\OperationContext\Web\Illuminate\IlluminateOperationContext as OperationContextAdapter;
15
use POData\SimpleDataService as DataService;
16
17
class ODataController extends BaseController
18
{
19
    /**
20
     * Display a listing of the resource.
21
     *
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index(Request $request)
25
    {
26
        $dump = $this->isDumping();
27
        $dryRun = $this->isDryRun();
28
        $commitCall = $dryRun ? 'rollBack' : 'commit';
29
30
        try {
31
            DB::beginTransaction();
32
            $context = new OperationContextAdapter($request);
33
            $host = new ServiceHost($context, $request);
34
            $host->setServiceUri('/odata.svc/');
35
36
            $query = App::make('odataquery');
37
            $meta = App::make('metadata');
38
39
            $service = new DataService($query, $meta, $host);
40
            $cereal = new IronicSerialiser($service, null);
41
            $service = new DataService($query, $meta, $host, $cereal);
42
            $pageSize = $this->getAppPageSize();
43
            if (null !== $pageSize) {
44
                $service->maxPageSize = intval($pageSize);
45
            }
46
            $service->handleRequest();
47
48
            $odataResponse = $context->outgoingResponse();
49
50
            if (true === $dump) {
51
                // iff XTest header is set, containing class and method name
52
                // dump outgoing odataResponse, metadata, and incoming request
53
                $xTest = $request->header('XTest');
54
                $date = Carbon::now(0);
55
                $timeString = $date->toTimeString();
56
                $xTest = (null !== $xTest) ? $xTest
57
                    : $request->method() . ';' . str_replace('/', '-', $request->path()) . ';' . $timeString . ';';
58
                if (null != $xTest) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $xTest of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
59
                    $reflectionClass = new \ReflectionClass('Illuminate\Http\Request');
60
                    $reflectionProperty = $reflectionClass->getProperty('userResolver');
61
                    $reflectionProperty->setAccessible(true);
62
                    $reflectionProperty->setValue($request, null);
63
                    $reflectionProperty = $reflectionClass->getProperty('routeResolver');
64
                    $reflectionProperty->setAccessible(true);
65
                    $reflectionProperty->setValue($request, null);
66
                    $cerealRequest = serialize($request);
67
                    $cerealMeta = serialize($meta);
68
                    $cerealResponse = serialize($odataResponse);
69
                    Storage::put($xTest . 'request', $cerealRequest);
70
                    Storage::put($xTest . 'metadata', $cerealMeta);
71
                    Storage::put($xTest . 'response', $cerealResponse);
72
                }
73
            }
74
75
            $content = $odataResponse->getStream();
76
77
            $headers = $odataResponse->getHeaders();
78
            $responseCode = $headers[\POData\Common\ODataConstants::HTTPRESPONSE_HEADER_STATUS_CODE];
79
            $responseCode = isset($responseCode) ? intval($responseCode) : 200;
80
            $response = new Response($content, $responseCode);
81
            $response->setStatusCode($headers['Status']);
0 ignored issues
show
Bug introduced by
$headers['Status'] of type string is incompatible with the type integer expected by parameter $code of Symfony\Component\HttpFo...sponse::setStatusCode(). ( Ignorable by Annotation )

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

81
            $response->setStatusCode(/** @scrutinizer ignore-type */ $headers['Status']);
Loading history...
82
83
            foreach ($headers as $headerName => $headerValue) {
84
                if (null !== $headerValue) {
85
                    $response->headers->set($headerName, $headerValue);
86
                }
87
            }
88
            DB::$commitCall();
89
        } catch (\Exception $e) {
90
            DB::rollBack();
91
            throw $e;
92
        }
93
        return $response;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    protected function isDumping()
100
    {
101
        $configDump = env('APP_DUMP_REQUESTS', false);
102
        return true === $configDump;
103
    }
104
105
    /**
106
     * Is application dry-running (ie, not committing) non-READ requests?
107
     *
108
     * @return bool
109
     */
110
    protected function isDryRun()
111
    {
112
        $configDump = env('APP_DRY_RUN', false);
113
        return true === $configDump;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    protected function getAppPageSize()
120
    {
121
        return env('APP_PAGE_SIZE', null);
122
    }
123
}
124