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

XmlApiResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 49
ccs 0
cts 21
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCode() 0 4 1
A send() 0 13 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