ErrorToExceptionFilter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 4 1
A afterAction() 0 16 5
A getErrorMessage() 0 11 3
1
<?php
2
3
namespace zacksleo\yii2\oauth2\api\filters;
4
5
use Yii;
6
use yii\base\Controller;
7
use filsh\yii2\oauth2server\Module;
8
use filsh\yii2\oauth2server\exceptions\HttpException;
9
use zacksleo\yii2\oauth2\api\Module as ApiModule;
10
11
class ErrorToExceptionFilter extends \yii\base\Behavior
12
{
13
    /**
14
     * @inheritdoc
15
     */
16
    public function events()
17
    {
18
        return [Controller::EVENT_AFTER_ACTION => 'afterAction'];
19
    }
20
21
    /**
22
     * @param ActionEvent $event
23
     * @return boolean
24
     * @throws HttpException when the request method is not allowed.
25
     */
26
    public function afterAction($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    {
28
        $response = Module::getInstance()->getServer()->getResponse();
29
30
        $isValid = true;
31
        if ($response !== null) {
32
            $isValid = $response->isInformational() || $response->isSuccessful() || $response->isRedirection();
33
        }
34
        if (!$isValid) {
35
            throw new HttpException(
36
                $response->getStatusCode(),
37
                $this->getErrorMessage($response),
38
                $response->getParameter('error_uri')
39
            );
40
        }
41
    }
42
43
    protected function getErrorMessage(\OAuth2\Response $response)
44
    {
45
        $message = ApiModule::t('oauth2server', $response->getParameter('error_description'));
46
        if ($message == 'Invalid username and password combination') {
47
            $message = ApiModule::t('oauth2server', 'Incorrect account or password');
48
        }
49
        if ($message === null) {
50
            $message = ApiModule::t('oauth2server', 'An internal server error occurred.');
51
        }
52
        return $message;
53
    }
54
}
55