Completed
Push — master ( 5fd6e4...a21112 )
by Tomas
12:54
created

ApiConsoleControl::formSucceeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.4286
cc 2
eloc 11
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Tomaj\NetteApi\Component;
4
5
use Nette\Application\UI\Control;
6
use Nette\Application\UI\Form;
7
use Nette\Http\Request;
8
use Tomaj\Form\Renderer\BootstrapRenderer;
9
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
10
use Tomaj\NetteApi\Authorization\BearerTokenAuthorization;
11
use Tomaj\NetteApi\Authorization\NoAuthorization;
12
use Tomaj\NetteApi\EndpointIdentifier;
13
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
14
use Tomaj\NetteApi\Misc\ConsoleRequest;
15
use Tomaj\NetteApi\Params\InputParam;
16
17
class ApiConsoleControl extends Control
18
{
19
    private $endpoint;
20
21
    private $handler;
22
23
    private $authorization;
24
25
    private $request;
26
27
    public function __construct(Request $request, EndpointIdentifier $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization)
28
    {
29
        parent::__construct(null, 'apiconsolecontrol');
30
        $this->endpoint = $endpoint;
31
        $this->handler = $handler;
32
        $this->authorization = $authorization;
33
        $this->request = $request;
34
    }
35
36
    public function render()
37
    {
38
        $this->template->setFile(__DIR__ . '/console.latte');
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. 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...
39
        $this->template->render();
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. 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
    }
41
42
    protected function createComponentConsoleForm()
0 ignored issues
show
Coding Style introduced by
createComponentConsoleForm 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...
43
    {
44
        $form = new Form();
45
46
        $defaults = [];
47
48
        $form->setRenderer(new BootstrapRenderer());
49
50
        $uri = $this->request->getUrl();
51
        $scheme = $uri->scheme;
52
        if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
53
            $scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'];
54
        }
55
        $url = $scheme . '://' . $uri->host . '/api/' . $this->endpoint->getUrl();
56
57
        $form->addText('api_url', 'Api Url');
58
        $defaults['api_url'] = $url;
59
60
        $form->addText('method', 'Method');
61
        $defaults['method'] = $this->endpoint->getMethod();
62
63
        if ($this->authorization instanceof BearerTokenAuthorization) {
64
            $form->addText('token', 'Token:')
65
                ->setAttribute('placeholder', 'napište token');
0 ignored issues
show
Documentation introduced by
'napište token' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        } elseif ($this->authorization instanceof NoAuthorization) {
67
            $form->addText('authorization', 'Authorization')
68
                ->setDisabled(true);
69
            $defaults['authorization'] = 'No authorization - global access';
70
        }
71
72
        $params = $this->handler->params();
73
        foreach ($params as $param) {
74
            $count = $param->isMulti() ? 5 : 1;
75
            for ($i = 0; $i < $count; $i++) {
76
                $key = $param->getKey();
77
                if ($param->isMulti()) {
78
                    $key = $key . '___' . $i;
79
                }
80
                $c = $form->addText($key, $this->getParamLabel($param));
81
                if ($param->getAvailableValues()) {
82
                    $c->setOption('description', 'available values: ' . implode(' | ', $param->getAvailableValues()));
83
                }
84
            }
85
        }
86
87
        $form->addSubmit('send', 'Otestuj')
88
            ->getControlPrototype()
89
            ->setName('button')
90
            ->setHtml('<i class="fa fa-cloud-upload"></i> Try api');
91
92
        $form->setDefaults($defaults);
93
94
        $form->onSuccess[] = array($this, 'formSucceeded');
95
        return $form;
96
    }
97
98
    private function getParamLabel(InputParam $param)
99
    {
100
        $title = $param->getKey();
101
        if ($param->isRequired()) {
102
            $title .= ' *';
103
        }
104
        return $title;
105
    }
106
107
    public function formSucceeded($form, $values)
108
    {
109
        $url = $values['api_url'];
110
111
        $token = false;
112
        if (isset($values['token'])) {
113
            $token = $values['token'];
114
            unset($values['token']);
115
        }
116
117
        $method = $values['method'];
118
        unset($values['method']);
119
120
        $consoleRequest = new ConsoleRequest($this->handler);
121
        $result = $consoleRequest->makeRequest($url, $method, $values, $token);
122
123
        $this->template->add('response', $result);
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. 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...
124
    }
125
}
126