Completed
Push — master ( 5d76d8...23accf )
by
unknown
04:14
created

RestlerExtended::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
namespace Aoe\Restler\System\Restler;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Restler\System\TYPO3\Cache as Typo3Cache;
29
use Luracast\Restler\Restler;
30
use Luracast\Restler\RestException;
31
use Exception;
32
33
/**
34
 * @package Restler
35
 */
36
class RestlerExtended extends Restler
37
{
38
    /**
39
     * @var Typo3Cache
40
     */
41
    private $typo3Cache;
42
43
44
45
    /***************************************************************************************************************************/
46
    /***************************************************************************************************************************/
47
    /* Block of methods, which MUST be overriden from parent-class (otherwise we can't use the TYPO3-caching-framework) ********/
48
    /***************************************************************************************************************************/
49
    /***************************************************************************************************************************/
50
    /**
51
     * Constructor
52
     *
53
     * @param Typo3Cache $typo3Cache
54
     * @param boolean $productionMode    When set to false, it will run in
55
     *                                   debug mode and parse the class files
56
     *                                   every time to map it to the URL
57
     *
58
     * @param boolean $refreshCache      will update the cache when set to true
59
     */
60
    public function __construct(Typo3Cache $typo3Cache, $productionMode = false, $refreshCache = false)
61
    {
62
        parent::__construct($productionMode, $refreshCache);
63
        $this->typo3Cache = $typo3Cache;
64
    }
65
66
    /**
67
     * Main function for processing the api request
68
     * and return the response
69
     *
70
     * @throws Exception     when the api service class is missing
71
     * @throws RestException to send error response
72
     */
73
    public function handle()
74
    {
75
        // get information about the REST-request
76
        $this->get();
77
78
        if ($this->requestMethod === 'GET' && $this->typo3Cache->hasCacheEntry($this->url, $_GET)) {
79
            return $this->handleRequestByTypo3Cache();
80
        }
81
82
        // if no cache exist: restler should handle the request
83
        return parent::handle();
84
    }
85
86
    /**
87
     * override postCall so that we can cache response via TYPO3-caching-framework - if it's possible
88
     */
89
    protected function postCall()
90
    {
91
        parent::postCall();
92
93
        if ($this->typo3Cache->isResponseCacheableByTypo3Cache($this->requestMethod, $this->apiMethodInfo->metadata)) {
94
            $this->typo3Cache->cacheResponseByTypo3Cache(
95
                $this->url,
96
                $_GET,
97
                $this->apiMethodInfo->metadata,
98
                $this->responseData,
99
                get_class($this->responseFormat),
100
                headers_list()
101
            );
102
        }
103
    }
104
105
106
107
    /***************************************************************************************************************************/
108
    /***************************************************************************************************************************/
109
    /* Block of methods, which does NOT override logic from parent-class *******************************************************/
110
    /***************************************************************************************************************************/
111
    /***************************************************************************************************************************/
112
    /**
113
     * @return string
114
     */
115
    private function handleRequestByTypo3Cache()
116
    {
117
        $cacheEntry = $this->typo3Cache->getCacheEntry($this->url, $_GET);
118
119
        if (count($cacheEntry['responseHeaders']) === 0) {
120
            // the cache is from an internal REST-API-call, so we must manually send some headers
121
            @header('Content-Type: application/json; charset=utf-8');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
122
            @header('Cache-Control: private, no-cache, no-store, must-revalidate');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
123
        } else {
124
            // set/manipulate headers
125
            foreach ($cacheEntry['responseHeaders'] as $responseHeader) {
126
                if (substr($responseHeader, 0, 8) === 'Expires:') {
127
                    if ($cacheEntry['frontendCacheExpires'] === 0) {
128
                        $expires = $cacheEntry['frontendCacheExpires'];
129
                    } else {
130
                        $expires = gmdate('D, d M Y H:i:s \G\M\T', time() + $cacheEntry['frontendCacheExpires']);
131
                    }
132
                    @header('Expires: '.$expires);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
133
                } else {
134
                    @header($responseHeader);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
135
                }
136
            }
137
        }
138
        @header('X-Cached-By-Typo3: 1');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
139
140
        // send data to client
141
        $this->responseData = $cacheEntry['responseData'];
142
        return $this->respond();
143
    }
144
}
145