ErrorHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 30
c 0
b 0
f 0
dl 0
loc 56
ccs 0
cts 30
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handleException() 0 46 7
1
<?php
2
3
namespace POData\Common;
4
5
6
use POData\Writers\Json\JsonODataV2Writer;
7
use POData\Writers\Atom\AtomODataWriter;
8
use POData\HttpProcessUtility;
9
use POData\IService;
10
11
/**
12
 * Class ErrorHandler
13
 * @package POData\Common
14
 */
15
class ErrorHandler
16
{
17
    /**
18
     * Common function to handle exceptions in the data service.
19
     * 
20
     * @param \Exception    $exception    exception
21
     * @param IService      $service service
22
     * 
23
     * @return void
24
     */
25
    public static function handleException($exception, IService $service)
26
    {
27
        $acceptTypesText = $service->getHost()->getRequestAccept();
28
        $responseContentType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $responseContentType is dead and can be removed.
Loading history...
29
        try {
30
            $responseContentType = HttpProcessUtility::selectMimeType(
31
                $acceptTypesText, 
32
                array(
33
                    MimeTypes::MIME_APPLICATION_XML,
34
                    MimeTypes::MIME_APPLICATION_JSON
35
                )
36
            );
37
        } catch (HttpHeaderFailure $exception) {
38
            $exception = new ODataException(
39
                $exception->getMessage(), 
40
                $exception->getStatusCode()
41
            );
42
        } catch (\Exception $exception) {
43
            // Never come here
44
        }
45
46
        if (is_null($responseContentType)) {
0 ignored issues
show
introduced by
The condition is_null($responseContentType) is always false.
Loading history...
47
            $responseContentType = MimeTypes::MIME_APPLICATION_XML;
48
        }
49
50
        if (!($exception instanceof ODataException)) {
51
            $exception = new ODataException($exception->getMessage(), HttpStatus::CODE_INTERNAL_SERVER_ERROR);
52
        }
53
54
        $service->getHost()->setResponseVersion(ODataConstants::DATASERVICEVERSION_1_DOT_0 . ';');
55
56
        // At this point all kind of exceptions will be converted 
57
        //to 'ODataException' 
58
        if ($exception->getStatusCode() == HttpStatus::CODE_NOT_MODIFIED) {
59
            $service->getHost()->setResponseStatusCode(HttpStatus::CODE_NOT_MODIFIED);
60
        } else {
61
            $service->getHost()->setResponseStatusCode($exception->getStatusCode());
62
            $service->getHost()->setResponseContentType($responseContentType);
63
            $responseBody = null;
64
            if (strcasecmp($responseContentType, MimeTypes::MIME_APPLICATION_XML) == 0) {
65
                $responseBody = AtomODataWriter::serializeException($exception, true);
66
            } else {
67
                $responseBody = JsonODataV2Writer::serializeException($exception, true);
68
            }
69
70
            $service->getHost()->getOperationContext()->outgoingResponse()->setStream($responseBody);
71
        }
72
    }
73
}