GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#10)
by Lukáš
07:03
created

ApproveControl::handleApprove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Lookyman\Nette\OAuth2\Server\UI;
6
7
use League\OAuth2\Server\AuthorizationServer;
8
use League\OAuth2\Server\Exception\OAuthServerException;
9
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
10
use Lookyman\Nette\OAuth2\Server\Psr7\ApplicationPsr7ResponseInterface;
11
use Nette\Application\AbortException;
12
use Nette\Application\IResponse;
13
use Nette\Application\UI\Control;
14
use Nette\Http\IResponse as HttpResponse;
15
use Nette\Http\Session;
16
use Nextras\Application\UI\SecuredLinksControlTrait;
17
use Psr\Log\LoggerAwareInterface;
18
use Psr\Log\LoggerAwareTrait;
19
20
/**
21
 * @method void onResponse(IResponse $response)
22
 */
23
final class ApproveControl extends Control implements LoggerAwareInterface
24
{
25
	use LoggerAwareTrait;
26
	use Psr7Trait;
27
	use SecuredLinksControlTrait;
28
29
	/**
30
	 * @var callable[]
31
	 */
32
	public $onResponse;
33
34
	/**
35
	 * @var AuthorizationRequest
36
	 */
37
	private $authorizationRequest;
38
39
	/**
40
	 * @var AuthorizationServer
41
	 */
42
	private $authorizationServer;
43
44
	/**
45
	 * @var Session
46
	 */
47
	private $session;
48
49
	/**
50
	 * @var string
51
	 */
52
	private $templateFile;
53
54
	public function __construct(AuthorizationServer $authorizationServer, Session $session, AuthorizationRequest $authorizationRequest)
55
	{
56
		parent::__construct();
57
		$this->authorizationServer = $authorizationServer;
58
		$this->session = $session;
59
		$this->authorizationRequest = $authorizationRequest;
60
		$this->templateFile = __DIR__ . '/templates/approve.latte';
61
	}
62
63
	public function render()
64
	{
65
		$this->template->setFile($this->templateFile);
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...
66
		$this->template->authorizationRequest = $this->authorizationRequest;
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...
Bug introduced by
Accessing authorizationRequest on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
		$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...
68
	}
69
70
	/**
71
	 * @secured
72
	 */
73
	public function handleApprove()
74
	{
75
		$this->authorizationRequest->setAuthorizationApproved(true);
76
		$this->completeAuthorizationRequest();
77
	}
78
79
	/**
80
	 * @secured
81
	 */
82
	public function handleDeny()
83
	{
84
		$this->authorizationRequest->setAuthorizationApproved(false);
85
		$this->completeAuthorizationRequest();
86
	}
87
88
	private function completeAuthorizationRequest()
89
	{
90
		$this->session->getSection(OAuth2Presenter::SESSION_NAMESPACE)->remove();
91
92
		$response = $this->createResponse();
93
		try {
94
			/** @var ApplicationPsr7ResponseInterface $response */
95
			$response = $this->authorizationServer->completeAuthorizationRequest($this->authorizationRequest, $response);
96
			$this->onResponse($response);
97
98
		} catch (AbortException $e) {
99
			throw $e;
100
101
		} catch (OAuthServerException $e) {
102
			/** @var ApplicationPsr7ResponseInterface $response */
103
			$response = $e->generateHttpResponse($response);
104
			$this->onResponse($response);
105
106
		} catch (\Throwable $e) {
107
			if ($this->logger) {
108
				$this->logger->error($e->getMessage(), ['exception' => $e]);
109
			}
110
			$body = $this->createStream();
111
			$body->write('Unknown error');
112
			$this->onResponse($response->withStatus(HttpResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body));
113
		}
114
	}
115
116
	public function setTemplateFile(string $file)
117
	{
118
		$this->templateFile = $file;
119
	}
120
121
}
122