Passed
Pull Request — master (#171)
by Alex
03:56
created

ODataController::isDumping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
        $dryRun = $this->isDryRun();
27
        $commitCall = $dryRun ? 'rollBack' : 'commit';
28
29
        try {
30
            DB::beginTransaction();
31
            $context = new OperationContextAdapter($request);
32
            $host = new ServiceHost($context, $request);
33
            $host->setServiceUri('/odata.svc/');
34
35
            $query = App::make('odataquery');
36
            $meta = App::make('metadata');
37
38
            $service = new DataService($query, $meta, $host);
39
            $cereal = new IronicSerialiser($service, null);
40
            $service = new DataService($query, $meta, $host, $cereal);
41
            $pageSize = $this->getAppPageSize();
42
            if (null !== $pageSize) {
43
                $service->maxPageSize = intval($pageSize);
44
            }
45
            $service->handleRequest();
46
47
            $odataResponse = $context->outgoingResponse();
48
49
            $content = $odataResponse->getStream();
50
51
            $headers = $odataResponse->getHeaders();
52
            $responseCode = $headers[\POData\Common\ODataConstants::HTTPRESPONSE_HEADER_STATUS_CODE];
53
            $responseCode = isset($responseCode) ? intval($responseCode) : 200;
54
            $response = new Response($content, $responseCode);
55
56
            foreach ($headers as $headerName => $headerValue) {
57
                if (null !== $headerValue) {
58
                    $response->headers->set($headerName, $headerValue);
59
                }
60
            }
61
            DB::$commitCall();
62
        } catch (\Exception $e) {
63
            DB::rollBack();
64
            throw $e;
65
        }
66
        return $response;
67
    }
68
69
    /**
70
     * Is application dry-running (ie, not committing) non-READ requests?
71
     *
72
     * @return bool
73
     */
74
    protected function isDryRun()
75
    {
76
        $configDump = env('APP_DRY_RUN', false);
77
        return true === $configDump;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    protected function getAppPageSize()
84
    {
85
        return env('APP_PAGE_SIZE', null);
86
    }
87
}
88