Passed
Push — master ( 17b30a...9c28de )
by Mihail
14:15
created

ActionSocialAuth::socialauth()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 8
nop 1
dl 0
loc 48
rs 5.5102
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Front\User;
4
5
use Apps\Model\Front\User\FormLogin;
6
use Apps\Model\Front\User\FormSocialAuth;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Exception\SyntaxException;
11
use Ffcms\Core\Helper\Type\Str;
12
use Ffcms\Core\Network\Request;
13
use Ffcms\Core\Network\Response;
14
15
/**
16
 * Trait ActionSocialAuth
17
 * @package Apps\Controller\Front\User
18
 * @property View $view
19
 * @property Response $response
20
 * @property Request $request
21
 * @method array getConfigs()
22
 */
23
trait ActionSocialAuth
24
{
25
26
    /**
27
     * Authorization in social networks over hybridauth layer. How its work:
28
     *  1. User visit actionSocialauth and initialize openid instance
29
     *  2. 3rd party software generate redirect to @api -> User::actionEndpoint() (as endpoint) where create hash's, tokens and other shit
30
     *  3. After successful auth on service user redirect back to actionSocialauth and we can work with $userIdentity if no exceptions catched.
31
     * Don't aks me "why did you do this sh@t"? I want to make container in User class, but this shit work only on direct call on endpoint.
32
     * @param string $provider
33
     * @return string
34
     * @throws ForbiddenException
35
     * @throws SyntaxException
36
     */
37
    public function socialauth(string $provider)
38
    {
39
        // get hybridauth instance
40
        /** @var \Hybrid_Auth $instance */
41
        $instance = App::$User->getOpenidInstance();
0 ignored issues
show
Bug introduced by
The method getOpenidInstance does only exist in Apps\ActiveRecord\User, but not in Ffcms\Core\Interfaces\iUser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
        if (!$instance) {
43
            throw new ForbiddenException(__('OpenID auth is disabled'));
44
        }
45
46
        // try to get user identity data from remove service
47
        $userIdentity = null;
48
        try {
49
            $adapter = $instance->authenticate($provider);
50
            $userIdentity = $adapter->getUserProfile();
51
        } catch (\Exception $e) {
52
            throw new SyntaxException(__('Authorization failed: %e%', ['e' => $e->getMessage()]));
53
        }
54
55
        // check if openid data provided
56
        if (!$userIdentity || Str::likeEmpty($userIdentity->identifier)) {
57
            throw new ForbiddenException(__('User data not provided!'));
58
        }
59
60
        // initialize model and pass user identity
61
        $model = new FormSocialAuth($provider, $userIdentity);
0 ignored issues
show
Documentation introduced by
$provider 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...
62
        // check if user is always registered
63
        if ($model->identityExists()) {
64
            $model->makeAuth();
65
            $this->response->redirect('/');
66
            return null;
67
        }
68
        // its a new identify, check if finish register form is submited
69
        if ($model->send() && $model->validate()) {
70
            if ($model->tryRegister()) {
71
                // registration is completed, lets open new session
72
                $loginModel = new FormLogin();
73
                $loginModel->openSession($model->_userObject);
0 ignored issues
show
Bug introduced by
It seems like $model->_userObject can be null; however, openSession() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
74
                $this->response->redirect('/'); // session is opened, refresh page
75
            } else { // something gonna wrong, lets notify user
76
                App::$Session->getFlashBag()->add('error', __('Login or email is always used on website'));
77
            }
78
        }
79
80
        // render output view
81
        return $this->view->render('social_signup', [
82
            'model' => $model
83
        ]);
84
    }
85
}
86