Completed
Push — master ( 9e89d5...89e339 )
by Tomas
11:24
created

ApiPresenter::renderList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Tomaj\NetteApi\Presenters;
4
5
use Nette\Application\Responses\JsonResponse;
6
use Nette\Application\UI\Presenter;
7
use Nette\Http\Response;
8
use Tomaj\NetteApi\ApiDecider;
9
use Tomaj\NetteApi\Misc\IpDetectorInterface;
10
use Tomaj\NetteApi\Params\ParamsProcessor;
11
12
class ApiPresenter extends Presenter
13
{
14
    /**
15
     * @var  ApiDecider @inject
16
     */
17
    public $apiDecider;
18
19
    /**
20
     * @var  IpDetectorInterface @inject
21
     */
22
    public $ipDetector;
23
24
    /**
25
     * Nette render default method
26
     *
27
     * @return void
28
     */
29
    public function renderDefault()
0 ignored issues
show
Coding Style introduced by
renderDefault uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $start = microtime(true);
32
33
        $this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*');
34
35
        $logger = null;
36
        if ($this->context->hasService('apiLogger')) {
0 ignored issues
show
Documentation introduced by
The property $context is declared private in Nette\Application\UI\Presenter. 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...
37
            $logger = $this->context->getService('apiLogger');
0 ignored issues
show
Documentation introduced by
The property $context is declared private in Nette\Application\UI\Presenter. 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...
38
        }
39
40
        // get handler
41
        $hand = $this->apiDecider->getApiHandler(
42
            $this->request->getMethod(),
0 ignored issues
show
Documentation introduced by
The property $request is declared private in Nette\Application\UI\Presenter. 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...
43
            $this->params['version'],
44
            $this->params['package'],
45
            $this->params['apiAction']
46
        );
47
        $handler = $hand['handler'];
48
        $authorization = $hand['authorization'];
49
50
        // check authorization
51
        if (!$authorization->authorized()) {
52
            $this->getHttpResponse()->setCode(Response::S403_FORBIDDEN);
53
            $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()]));
54
            return;
55
        }
56
57
        // process params
58
        $paramsProcessor = new ParamsProcessor($handler->params());
59
        if ($paramsProcessor->isError()) {
60
            $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR);
61
            $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input']));
62
            return;
63
        }
64
        $params = $paramsProcessor->getValues();
65
66
        // process handler
67
        $response = $handler->handle($params);
68
        $code = $response->getCode();
69
70
        $end = microtime(true);
71
72
        if ($logger) {
73
            $headers = [];
74
            if (function_exists('getallheaders')) {
75
                $headers = getallheaders();
76
            }
77
78
            $requestHeaders = '';
79
            foreach ($headers as $key => $value) {
80
                $requestHeaders .= "$key: $value\n";
81
            }
82
83
            $logger->log(
84
                $code,
85
                $this->request->getMethod(),
0 ignored issues
show
Documentation introduced by
The property $request is declared private in Nette\Application\UI\Presenter. 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...
86
                $requestHeaders,
87
                $_SERVER['REQUEST_URI'],
88
                $this->ipDetector->getRequestIp(),
89
                isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
90
                ($end-$start) * 1000
91
            );
92
        }
93
94
        // output to nette
95
        $this->getHttpResponse()->setCode($code);
96
        $this->sendResponse($response);
97
    }
98
}
99