Completed
Push — master ( abe765...8b5726 )
by Tomas
10:33
created

XmlApiResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 3
    public function __construct($code, $data)
28
    {
29 3
        $this->code = $code;
30 3
        $this->response = $data;
31 3
    }
32
33
    /**
34
     * Return api response http code
35
     *
36
     * @return integer
37
     */
38 3
    public function getCode()
39
    {
40 3
        return $this->code;
41
    }
42
43
    public function send(IRequest $httpRequest, IResponse $httpResponse)
44
    {
45
        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...
46
            echo 'Generate or set response first';
47
            return;
48
        }
49
50
        $httpResponse->setContentType('text/xml');
51
        $httpResponse->setExpiration(false);
52
        $httpResponse->setCode($this->getCode());
53
54
        echo $this->response;
55
    }
56
}
57