Completed
Push — master ( bf8f23...c9e25b )
by Tomas
04:17
created

XmlApiResponse::getCode()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Tomaj\NetteApi\Response;
4
5
use Nette\Application\IResponse as ApplicationIResponse;
6
use Nette\Http\IRequest;
7
use Nette\Http\IResponse;
8
9
class XmlApiResponse implements ApplicationIResponse
10
{
11
    /**
12
     * @var integer
13
     */
14
    private $code;
15
16
    /** @var null|string  */
17
    private $response = null;
18
19
    /**
20
     * Create XmlApiResponse
21
     * This class only wrap JsonResponse from Nette and add possibility
22
     * to setup response code and automaticaly set content type
23
     *
24
     * @param integer $code
25
     * @param mixed $data
26
     */
27
    public function __construct($code, $data)
28
    {
29
        parent::__construct($data);
30
        $this->code = $code;
31
        $this->response = $data;
32
    }
33
34
    /**
35
     * Return api response http code
36
     *
37
     * @return integer
38
     */
39
    public function getCode()
40
    {
41
        return $this->code;
42
    }
43
44
    public function send(IRequest $httpRequest, IResponse $httpResponse)
45
    {
46
        if (!$this->response) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->response of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47
            echo 'Generate or set response first';
48
            return;
49
        }
50
51
        $httpResponse->setContentType('text/xml');
52
        $httpResponse->setExpiration(false);
53
        $httpResponse->setCode($this->getCode());
54
55
        echo $this->response;
56
    }
57
}
58