Completed
Push — master ( d339f3...382f5f )
by Tomas
05:14
created

JsonApiResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 60
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCode() 0 4 1
A getCharset() 0 4 1
A send() 0 6 1
1
<?php
2
3
namespace Tomaj\NetteApi\Response;
4
5
use Nette\Application\Responses\JsonResponse;
6
use Nette;
7
8
class JsonApiResponse extends JsonResponse
9
{
10
    /**
11
     * @var integer
12
     */
13
    private $code;
14
15
    /**
16
     * @var string
17
     */
18
    private $charset;
19
20
    /**
21
     * Create JsonApiResponse
22
     * This class only wrap JsonResponse from Nette and add possibility
23
     * to setup response code and automaticaly set content type
24
     *
25
     * @param integer $code
26
     * @param mixed $data
27
     * @param string $contentType
28
     * @param string $charset
29
     */
30 18
    public function __construct($code, $data, $contentType = 'application/json', $charset ='utf-8')
31
    {
32 18
        parent::__construct($data, $contentType);
33 18
        $this->charset = $charset;
34 18
        $this->code = $code;
35 18
    }
36
37
    /**
38
     * Return api response http code
39
     *
40
     * @return integer
41
     */
42 18
    public function getCode()
43
    {
44 18
        return $this->code;
45
    }
46
47
    /**
48
     * Return encoding charset for http response
49
     *
50
     * @return string
51
     */
52 6
    public function getCharset()
53
    {
54 6
        return $this->charset;
55
    }
56
57
    /**
58
     * Sends response to output.
59
     * @return void
60
     */
61 3
    public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
62
    {
63 3
        $httpResponse->setContentType($this->contentType, $this->charset);
0 ignored issues
show
Documentation introduced by
The property $contentType is declared private in Nette\Application\Responses\JsonResponse. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64 3
        $httpResponse->setExpiration(FALSE);
65 3
        echo Nette\Utils\Json::encode($this->payload);
0 ignored issues
show
Documentation introduced by
The property $payload is declared private in Nette\Application\Responses\JsonResponse. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66 3
    }
67
}
68