Facebook   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 123
rs 8.3157
c 0
b 0
f 0
wmc 43

1 Method

Rating   Name   Duplication   Size   Complexity  
F auth() 0 121 43

How to fix   Complexity   

Complex Class

Complex classes like Facebook often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Facebook, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Social helper vk
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Users\SocialHelper;
13
14
class Facebook extends \Users\SocialHelper {
15
16
    public static function auth() {
17
        $config = static::getConfig();
18
        if (empty($_GET['code']) && empty($_GET['error'])) {
19
            $query = [
20
                'client_id' => $config['appId'],
21
                'scope' => 'email',
22
                'response_type' => 'code',
23
                'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/facebook'
24
            ];
25
            \Inji\Tools::redirect("https://www.facebook.com/dialog/oauth?" . http_build_query($query));
26
        }
27
        if (empty($_GET['code']) && !empty($_GET['error'])) {
28
            \Inji\Tools::redirect('/', 'Произошла ошибка во время авторизации через соц. сеть: ' . $_GET['error_description']);
29
        }
30
        $query = [
31
            'client_id' => $config['appId'],
32
            'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/facebook',
33
            'client_secret' => $config['secret'],
34
            'code' => urldecode($_GET['code']),
35
        ];
36
        $result = @file_get_contents("https://graph.facebook.com/oauth/access_token?" . http_build_query($query));
37
        if ($result === false) {
38
            \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger');
39
        }
40
        parse_str($result, $output);
41
        if (empty($output['access_token'])) {
42
            \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger');
43
        }
44
        $userQuery = [
45
            'access_token' => $output['access_token'],
46
            'fields' => 'first_name,middle_name,last_name,email,gender,location,picture'
47
        ];
48
        $userResult = @file_get_contents("https://graph.facebook.com/me?" . http_build_query($userQuery));
49
        if (!$userResult) {
50
            \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger');
51
        }
52
        $userDetail = json_decode($userResult, true);
53
        if (empty($userDetail['id'])) {
54
            \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger');
55
        }
56
        $social = static::getObject();
57
        $userSocial = \Users\User\Social::get([['uid', $userDetail['id']], ['social_id', $social->id]]);
58
        if ($userSocial && $userSocial->user) {
59
            \App::$cur->users->newSession($userSocial->user);
60
            if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) {
61
                \Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]);
62
            }
63
        } else {
64
            if ($userSocial && !$userSocial->user) {
65
                $userSocial->delete();
66
            }
67
            if (!\Users\User::$cur->id) {
68
                $user = false;
69
                if (!empty($userDetail['email'])) {
70
                    $user = \Users\User::get($userDetail['email'], 'mail');
71
                }
72
                if (!$user) {
73
                    $user = new \Users\User();
74
                    $user->group_id = 2;
75
                    $user->role_id = 2;
76
                    if (!empty($userDetail['email'])) {
77
                        $user->login = $user->mail = $userDetail['email'];
78
                    }
79
                    $invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : '')))));
80
                    if (!empty($invite_code)) {
81
                        $invite = \Users\User\Invite::get($invite_code, 'code');
82
                        $inveiteError = false;
83
                        if (!$invite) {
84
                            \Inji\Msg::add('Такой код пришлашения не найден', 'danger');
85
                            $inveiteError = true;
86
                        }
87
                        if ($invite->limit && !($invite->limit - $invite->count)) {
0 ignored issues
show
Bug introduced by
The property limit does not exist on false.
Loading history...
Bug introduced by
The property count does not exist on false.
Loading history...
88
                            \Inji\Msg::add('Лимит приглашений для данного кода исчерпан', 'danger');
89
                            $inveiteError = true;
90
                        }
91
                        if (!$inveiteError) {
92
                            $user->parent_id = $invite->user_id;
93
                            $invite->count++;
94
                            $invite->save();
95
                        }
96
                    }
97
                    if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) {
98
                        $user->parent_id = \App::$cur->Users->config['defaultPartner'];
99
                    }
100
                    $user->save();
101
                    $userInfo = new \Users\User\Info();
102
                    $userInfo->user_id = $user->id;
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist on Users\User\Info. Since you implemented __set, consider adding a @property annotation.
Loading history...
103
                    $userInfo->save();
104
                }
105
            } else {
106
                $user = \Users\User::$cur;
107
            }
108
            if (!$user->info->photo_file_id && !empty($userDetail['picture']['data']['url'])) {
109
                $user->info->photo_file_id = \App::$cur->files->uploadFromUrl($userDetail['picture']['data']['url']);
110
            }
111
            if (!$user->info->first_name && !empty($userDetail['first_name'])) {
112
                $user->info->first_name = $userDetail['first_name'];
113
            }
114
            if (!$user->info->last_name && !empty($userDetail['last_name'])) {
115
                $user->info->last_name = $userDetail['last_name'];
116
            }
117
            if (!$user->info->middle_name && !empty($userDetail['middle_name'])) {
118
                $user->info->middle_name = $userDetail['middle_name'];
119
            }
120
            if (!$user->info->city && !empty($userDetail['location'])) {
121
                $user->info->city = $userDetail['location'];
122
            }
123
            if (!$user->info->sex && !empty($userDetail['gender'])) {
124
                $user->info->sex = $userDetail['gender'] == 'male' ? 1 : ($userDetail['gender'] == 'female' ? 2 : 0);
125
            }
126
            $user->info->save();
127
            $userSocial = new \Users\User\Social();
128
            $userSocial->uid = $userDetail['id'];
0 ignored issues
show
Bug Best Practice introduced by
The property uid does not exist on Users\User\Social. Since you implemented __set, consider adding a @property annotation.
Loading history...
129
            $userSocial->social_id = $social->id;
0 ignored issues
show
Bug Best Practice introduced by
The property social_id does not exist on Users\User\Social. Since you implemented __set, consider adding a @property annotation.
Loading history...
130
            $userSocial->user_id = $user->id;
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist on Users\User\Social. Since you implemented __set, consider adding a @property annotation.
Loading history...
131
            $userSocial->save();
132
            \App::$cur->users->newSession($user);
133
            if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) {
134
                \Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type], 'Вы успешно зарегистрировались через Facebook', 'success');
135
            } else {
136
                \Inji\Tools::redirect('/users/cabinet/profile', 'Вы успешно зарегистрировались через Facebook', 'success');
137
            }
138
        }
139
    }
140
141
}
142