AuthPresenter   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 189
c 2
b 0
f 0
rs 10
wmc 17
lcom 1
cbo 7

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthService() 0 4 1
A setAuthService() 0 6 1
A getUserService() 0 4 1
A setUserService() 0 6 1
A __construct() 0 7 1
A startup() 0 4 1
A actionLogin() 0 4 1
A actionLogout() 0 4 1
A actionSkautis() 0 4 1
A skautisLogin() 0 4 1
A skautisLogout() 0 4 1
B handleSkautisLogin() 0 33 5
A handleSkautisLogout() 0 15 1
1
<?php
2
3
namespace App\Presenters;
4
5
use Skautis;
6
use SkautisAuth;
7
use App\Services\SkautIS\AuthService;
8
use App\Services\SkautIS\UserService;
9
use Tracy\Debugger;
10
11
/**
12
 * SkautIS Auth presenters.
13
 */
14
class AuthPresenter extends BasePresenter
15
{
16
17
	/**
18
	 * @var AuthService
19
	 */
20
	protected $authService;
21
22
	/**
23
	 * @var UserService
24
	 */
25
	protected $userService;
26
27
	/**
28
	 * @var User
29
	 */
30
	protected $user;
31
32
	/**
33
	 * @param AuthService $authService
34
	 * @param UserService $userService
35
	 */
36
	public function __construct(
37
		AuthService $authService,
38
		UserService $userService
39
	) {
40
		$this->setAuthService($authService);
41
		$this->setUserService($userService);
42
	}
43
44
	/**
45
	 * @return void
46
	 */
47
	protected function startup()
48
	{
49
		parent::startup();
50
	}
51
52
	/**
53
	 * @param  string $id
54
	 * @return void
55
	 */
56
	public function actionLogin($id)
57
	{
58
		$this->{$id . 'Login'}();
59
	}
60
61
	/**
62
	 * @param  string $id
63
	 * @return void
64
	 */
65
	public function actionLogout($id)
66
	{
67
		$this->{$id . 'Logout'}();
68
	}
69
70
	/**
71
	 * @param  string $id
72
	 * @return void
73
	 */
74
	public function actionSkautis($id)
75
	{
76
		$this->{'handleSkautis' . $id}();
77
	}
78
79
	/**
80
	 * Redirects to login page
81
	 *
82
	 * @param  	string  $backlink
83
	 * @return  void
84
	 */
85
	protected function skautisLogin($backlink = null)
86
	{
87
		$this->redirectUrl($this->getAuthService()->getLoginUrl($backlink));
88
	}
89
90
	/**
91
	 * Handle log out from SkautIS
92
	 * SkautIS redirects to this action after log out
93
	 *
94
	 * @param   void
95
	 * @return  void
96
	 */
97
	protected function skautisLogout()
98
	{
99
		$this->redirectUrl($this->getAuthService()->getLogoutUrl());
100
	}
101
102
	/**
103
	 * Handle SkautIS login process
104
	 *
105
	 * @param   string  $ReturnUrl
106
	 * @return  void
107
	 */
108
	protected function handleSkautisLogin($ReturnUrl = NULL)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $ReturnUrl is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
109
	{
110
		$post = $this->getHttpRequest()->post;
0 ignored issues
show
Bug introduced by
Accessing post on the interface Nette\Http\IRequest 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...
111
		//$post = $this->router->getPost();
112
		// if token is not set - get out from here - must log in
113
		if (!isset($post['skautIS_Token'])) {
114
			$this->redirect(":Login:");
115
		}
116
		//Debugger::log("AuthP: ".$post['skautIS_Token']." / ". $post['skautIS_IDRole'] . " / " . $post['skautIS_IDUnit'], "auth");
117
		try {
118
			$this->getAuthService()->setInit($post);
119
			//$this->container->getService('authService')->setInit($post);
120
121
			if (!$this->getUserService()->isLoggedIn()) {
122
			//if (!$this->container->getService('userService')->isLoggedIn()) {
123
				throw new \Skautis\Wsdl\AuthenticationException("Nemáte platné přihlášení do skautISu");
124
			}
125
			$me = $this->getUserService()->getPersonalDetail();
126
			//$me = $this->container->getService('userService')->getPersonalDetail();
127
128
			$this->getUser()->setExpiration('+ 29 minutes'); // nastavíme expiraci
129
			$this->getUser()->setAuthenticator(new SkautisAuth\SkautisAuthenticator());
130
			$this->getUser()->login($me);
131
132
			if (isset($ReturnUrl)) {
133
				$this->context->application->restoreRequest($ReturnUrl);
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...
134
			}
135
		} catch (\Skautis\Wsdl\AuthenticationException $e) {
136
			$this->flashMessage($e->getMessage(), "danger");
137
			$this->redirect(":Auth:Login");
138
		}
139
		$this->presenter->redirect(':Registration:');
140
	}
141
142
	/**
143
	 * Log out from SkautIS
144
	 *
145
	 * @param   void
146
	 * @return  void
147
	 */
148
	protected function handleSkautisLogout()
149
	{
150
		$this->getUserService()->logout(TRUE);
0 ignored issues
show
Documentation Bug introduced by
The method logout does not exist on object<App\Services\SkautIS\UserService>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
151
		$this->getUserService()->resetLoginData();
152
		$this->presenter->flashMessage("Byl jsi úspěšně odhlášen ze SkautISu.");
153
		/*
154
		if ($this->request->post['skautIS_Logout']) {
155
			$this->presenter->flashMessage("Byl jsi úspěšně odhlášen ze SkautISu.");
156
		} else {
157
			$this->presenter->flashMessage("Odhlášení ze skautISu se nezdařilo", "danger");
158
		}
159
		*/
160
		$this->redirect(":Login:");
161
		//$this->redirectUrl($this->service->getLogoutUrl());
162
	}
163
164
	/**
165
	 * @return AuthService
166
	 */
167
	protected function getAuthService()
168
	{
169
		return $this->authService;
170
	}
171
172
	/**
173
	 * @param  AuthService $service
174
	 * @return $this
175
	 */
176
	protected function setAuthService(AuthService $service)
177
	{
178
		$this->authService = $service;
179
180
		return $this;
181
	}
182
183
	/**
184
	 * @return UserService
185
	 */
186
	protected function getUserService()
187
	{
188
		return $this->userService;
189
	}
190
191
	/**
192
	 * @param  UserService $service
193
	 * @return $this
194
	 */
195
	protected function setUserService(UserService $service)
196
	{
197
		$this->userService = $service;
198
199
		return $this;
200
	}
201
202
}
203