OAuthControllerTrait::with()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 22
rs 9.8333
ccs 0
cts 13
cp 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace ByTIC\Hello\Modules\Frontend\Controllers\Traits;
4
5
use ByTIC\Hello\Models\Users\Logins\Traits\LoginTrait;
6
use Exception;
7
use Hybrid_Endpoint;
8
use ByTIC\Hello\Library\Hybridauth\Hybridauth;
9
use Nip\Controllers\Traits\AbstractControllerTrait;
10
use Nip\Records\Locator\ModelLocator;
11
12
/**
13
 * Trait OAuthControllerTrait
14
 * @package ByTIC\Hello\Modules\Frontend\Controllers\Traits
15
 */
16
trait OAuthControllerTrait
17
{
18
    use AbstractControllerTrait;
19
20
    /**
21
     * @return Hybrid_Endpoint
22
     */
23
    public function index()
24
    {
25
        return Hybrid_Endpoint::process();
26
    }
27
28
    public function link()
29
    {
30
        $providerName = $_REQUEST["provider"];
31
        $userProfile = Hybridauth::instance()->authenticate($providerName);
32
33
        $this->_getUser()->first_name = $userProfile->firstName;
0 ignored issues
show
Bug introduced by
It seems like _getUser() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $this->/** @scrutinizer ignore-call */ 
34
               _getUser()->first_name = $userProfile->firstName;
Loading history...
34
        $this->_getUser()->last_name = $userProfile->lastName;
35
        $this->_getUser()->email = $userProfile->email;
36
37
        $this->getView()->set('headerTitle', ModelLocator::get('users')->getLabel('o_auth_link.title'));
38
39
        foreach (['login', 'register'] as $userAction) {
40
            $form = $this->_getUser()->getForm($userAction);
41
42
            if ($form->execute()) {
43
                /** @var LoginTrait $userLogin */
44
                ModelLocator::get('users-logins')
45
                    ->createForProvider($providerName, $this->_getUser(), $userProfile);
46
47
                $this->doSuccessRedirect($userAction);
0 ignored issues
show
Bug introduced by
It seems like doSuccessRedirect() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                $this->/** @scrutinizer ignore-call */ 
48
                       doSuccessRedirect($userAction);
Loading history...
48
            }
49
            $this->forms[$userAction] = $form;
0 ignored issues
show
Bug Best Practice introduced by
The property forms does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        }
51
52
        $this->_setMeta('login');
0 ignored issues
show
Bug introduced by
It seems like _setMeta() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $this->/** @scrutinizer ignore-call */ 
53
               _setMeta('login');
Loading history...
53
    }
54
55
    public function with()
56
    {
57
        $providerName = $this->getRequest()->get('provider');
58
        $userProfile = Hybridauth::instance()->authenticate($providerName);
59
60
        if ($userProfile instanceof Exception) {
0 ignored issues
show
introduced by
$userProfile is never a sub-type of Exception.
Loading history...
61
            $this->getView()->set('exception', $userProfile);
62
        } else {
63
            $userExist = ModelLocator::get('users-logins')->getUserByProvider($providerName, $userProfile->identifier);
64
65
            if (!$userExist) {
66
                $this->redirect($this->Url()->assemble(
0 ignored issues
show
Bug introduced by
It seems like Url() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
                $this->redirect($this->/** @scrutinizer ignore-call */ Url()->assemble(
Loading history...
67
                    'frontend.o_auth.link',
68
                    [
69
                        'provider' => $providerName,
70
                        'redirect' => $this->getRedirectURL(),
0 ignored issues
show
Bug introduced by
It seems like getRedirectURL() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                        'redirect' => $this->/** @scrutinizer ignore-call */ getRedirectURL(),
Loading history...
71
                    ]
72
                ));
73
            }
74
75
            $userExist->doAuthentication();
0 ignored issues
show
Bug introduced by
The method doAuthentication() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            $userExist->/** @scrutinizer ignore-call */ 
76
                        doAuthentication();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            $this->doSuccessRedirect();
77
        }
78
    }
79
}
80