AuthController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 88
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionLogin() 0 3 1
A actionInfo() 0 3 1
A actions() 0 6 1
A updateImage() 0 10 3
A authSuccess() 0 32 4
A actionLogout() 0 5 1
A behaviors() 0 7 1
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 26.01.2018
6
 */
7
8
namespace app\modules\admin\controllers;
9
10
11
use app\components\ArrayHelper;
12
use app\models\User;
13
use Yii;
14
use yii\authclient\AuthAction;
15
use yii\authclient\ClientInterface;
16
use yii\filters\VerbFilter;
17
use yii\web\Controller;
18
19
class AuthController extends Controller
20
{
21
    public $layout = 'main-auth';
22
    public $defaultAction = 'login';
23
24
    public function behaviors()
25
    {
26
        return [
27
            'verbs' => [
28
                'class' => VerbFilter::class,
29
                'actions' => [
30
                    'logout' => ['post'],
31
                ],
32
            ],
33
        ];
34
    }
35
36
    public function actionLogin()
37
    {
38
        return $this->render('login');
39
    }
40
41
    public function actionLogout()
42
    {
43
        Yii::$app->user->logout();
0 ignored issues
show
Bug introduced by
The method logout() does not exist on null. ( Ignorable by Annotation )

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

43
        Yii::$app->user->/** @scrutinizer ignore-call */ 
44
                         logout();

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...
44
45
        return $this->goHome();
46
    }
47
48
    public function actionInfo()
49
    {
50
        return $this->render('info');
51
    }
52
53
    public function actions()
54
    {
55
        return [
56
            'auth' => [
57
                'class' => AuthAction::class,
58
                'successCallback' => [$this, 'authSuccess'],
59
            ],
60
        ];
61
    }
62
63
    public function authSuccess(ClientInterface $client)
64
    {
65
        $attributes = $client->getUserAttributes();
66
        $googleId = ArrayHelper::getValue($attributes, 'id');
67
        $email = ArrayHelper::getValue($attributes, 'email');
68
        $username = explode('@', $email)['0'];
69
70
        $user = User::find()
71
            ->andWhere([
72
                'google_user_id' => $googleId,
73
                'email' => $email,
74
            ])
75
            ->one();
76
77
        if ($user === null) {
78
            $user = new User([
79
                'google_user_id' => $googleId,
80
                'email' => $email,
81
                'username' => $username,
82
            ]);
83
            if ($user->save()) {
84
                $imageUrl = ArrayHelper::getValue($attributes, 'picture');
85
                $this->updateImage($imageUrl, $username, $user);
0 ignored issues
show
Bug introduced by
It seems like $imageUrl can also be of type null; however, parameter $image of app\modules\admin\contro...ntroller::updateImage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

85
                $this->updateImage(/** @scrutinizer ignore-type */ $imageUrl, $username, $user);
Loading history...
86
            }
87
        }
88
89
        if (!$user->active) {
90
            //TODO dodac info, ze admin aktywuje konto
91
            return $this->redirect(['auth/info']);
92
        }
93
94
        Yii::$app->user->login($user);
95
    }
96
97
    private function updateImage(string $image, string $username, User $user): void
98
    {
99
        $content = @file_get_contents($image);
100
        if ($content) {
101
            $uid = sprintf("%s_%s", $username, md5($image));
102
            $ext = explode('?', pathinfo($image, PATHINFO_EXTENSION))['0'];
103
            $path = Yii::getAlias("@webroot/uploads/{$uid}.{$ext}");
104
            if (file_put_contents($path, $content)) {
105
                $user->image = "/uploads/{$uid}.{$ext}";
106
                $user->update(false);
107
            }
108
        }
109
    }
110
}