Completed
Push — master ( 54781a...616953 )
by Tomas
02:20
created

ApiPresenter::renderDefault()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 69
Code Lines 43

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 69
rs 6.5437
cc 8
eloc 43
nc 10
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /** @var  ApiDecider @inject */
15
    public $apiDecider;
16
17
    /** @var  IpDetectorInterface @inject */
18
    public $ipDetector;
19
20
    public function renderList($version = null)
21
    {
22
        $list = $this->apiDecider->getHandlersList($version);
23
        $this->sendJson($list);
24
    }
25
    
26
    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...
27
    {
28
        $start = microtime(true);
29
30
        $this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*');
31
32
        $logger = null;
33
        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...
34
            $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...
35
        }
36
37
        // get handler
38
        $hand = $this->apiDecider->getApiHandler(
39
            $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...
40
            $this->params['version'],
41
            $this->params['package'],
42
            $this->params['apiAction']
43
        );
44
        $handler = $hand['handler'];
45
        $authorization = $hand['authorization'];
46
47
        // check authorization
48
        if (!$authorization->authorized()) {
49
            $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR);
50
            $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()]));
51
            return;
52
        }
53
54
        // process params
55
        $paramsProcessor = new ParamsProcessor($handler->params());
56
        if ($paramsProcessor->isError()) {
57
            $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR);
58
            $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input']));
59
            return;
60
        }
61
        $params = $paramsProcessor->getValues();
62
63
        // process handler
64
        $response = $handler->handle($params);
65
        $code = $response->getCode();
66
67
        $end = microtime(true);
68
69
        if ($logger) {
70
            $headers = [];
71
            if (function_exists('getallheaders')) {
72
                $headers = getallheaders();
73
            }
74
75
            $requestHeaders = '';
76
            foreach ($headers as $key => $value) {
77
                $requestHeaders .= "$key: $value\n";
78
            }
79
80
            $logger->log(
81
                $code,
82
                $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...
83
                $requestHeaders,
84
                $_SERVER['REQUEST_URI'],
85
                $this->ipDetector->getRequestIp(),
86
                isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
87
                ($end-$start) * 1000
88
            );
89
        }
90
91
        // output to nette
92
        $this->getHttpResponse()->setCode($code);
93
        $this->sendResponse($response);
94
    }
95
}
96