| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 120 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | 'display' => 'page', |
||
| 24 | 'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/vk' |
||
| 25 | ]; |
||
| 26 | \Inji\Tools::redirect("https://oauth.vk.com/authorize?" . http_build_query($query)); |
||
| 27 | } |
||
| 28 | if (empty($_GET['code']) && !empty($_GET['error'])) { |
||
| 29 | \Inji\Tools::redirect('/', 'Произошла ошибка во время авторизации через соц. сеть: ' . $_GET['error_description']); |
||
| 30 | } |
||
| 31 | $query = [ |
||
| 32 | 'client_id' => $config['appId'], |
||
| 33 | 'client_secret' => $config['secret'], |
||
| 34 | 'code' => $_GET['code'], |
||
| 35 | 'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/vk' |
||
| 36 | ]; |
||
| 37 | $result = @file_get_contents("https://oauth.vk.com/access_token?" . http_build_query($query)); |
||
| 38 | if ($result === false) { |
||
| 39 | \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
||
| 40 | } |
||
| 41 | $result = json_decode($result, true); |
||
| 42 | if (empty($result['user_id'])) { |
||
| 43 | \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
||
| 44 | } |
||
| 45 | $userQuery = [ |
||
| 46 | 'user_id' => $result['user_id'], |
||
| 47 | 'fields' => 'sex, bdate, photo_max_orig, home_town', |
||
| 48 | 'access_token' => $result['access_token'] |
||
| 49 | ]; |
||
| 50 | $userResult = @file_get_contents("https://api.vk.com/method/users.get?" . http_build_query($userQuery)); |
||
| 51 | if (!$userResult) { |
||
| 52 | \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
||
| 53 | } |
||
| 54 | $userDetail = json_decode($userResult, true); |
||
| 55 | if (empty($userDetail['response'][0])) { |
||
| 56 | \Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
||
| 57 | } |
||
| 58 | $social = static::getObject(); |
||
| 59 | $userSocial = \Users\User\Social::get([['uid', $result['user_id']], ['social_id', $social->id]]); |
||
| 60 | if ($userSocial && $userSocial->user) { |
||
| 61 | \App::$cur->users->newSession($userSocial->user); |
||
| 62 | if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) { |
||
| 63 | \Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]); |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | if ($userSocial && !$userSocial->user) { |
||
| 67 | $userSocial->delete(); |
||
| 68 | } |
||
| 69 | if (!\Users\User::$cur->id) { |
||
| 70 | $user = false; |
||
| 71 | if (!empty($result['email'])) { |
||
| 72 | $user = \Users\User::get($result['email'], 'mail'); |
||
| 73 | } |
||
| 74 | if (!$user) { |
||
| 75 | $user = new \Users\User(); |
||
| 76 | $user->group_id = 2; |
||
| 77 | $user->role_id = 2; |
||
| 78 | if (!empty($result['email'])) { |
||
| 79 | $user->login = $user->mail = $result['email']; |
||
| 80 | } |
||
| 81 | $invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : ''))))); |
||
| 82 | if (!empty($invite_code)) { |
||
| 83 | $invite = \Users\User\Invite::get($invite_code, 'code'); |
||
| 84 | $inveiteError = false; |
||
| 85 | if (!$invite) { |
||
| 86 | \Inji\Msg::add('Такой код пришлашения не найден', 'danger'); |
||
| 87 | $inveiteError = true; |
||
| 88 | } |
||
| 89 | if ($invite->limit && !($invite->limit - $invite->count)) { |
||
| 90 | \Inji\Msg::add('Лимит приглашений для данного кода исчерпан', 'danger'); |
||
| 91 | $inveiteError = true; |
||
| 92 | } |
||
| 93 | if (!$inveiteError) { |
||
| 94 | $user->parent_id = $invite->user_id; |
||
| 95 | $invite->count++; |
||
| 96 | $invite->save(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) { |
||
| 100 | $user->parent_id = \App::$cur->Users->config['defaultPartner']; |
||
| 101 | } |
||
| 102 | $user->save(); |
||
| 103 | $userInfo = new \Users\User\Info(); |
||
| 104 | $userInfo->user_id = $user->id; |
||
| 105 | $userInfo->save(); |
||
| 106 | } |
||
| 107 | } else { |
||
| 108 | $user = \Users\User::$cur; |
||
| 109 | } |
||
| 110 | if (!$user->info->photo_file_id && !empty($userDetail['response'][0]['photo_max_orig'])) { |
||
| 111 | $user->info->photo_file_id = \App::$cur->files->uploadFromUrl($userDetail['response'][0]['photo_max_orig']); |
||
| 112 | } |
||
| 113 | if (!$user->info->first_name && !empty($userDetail['response'][0]['first_name'])) { |
||
| 114 | $user->info->first_name = $userDetail['response'][0]['first_name']; |
||
| 115 | } |
||
| 116 | if (!$user->info->last_name && !empty($userDetail['response'][0]['last_name'])) { |
||
| 117 | $user->info->last_name = $userDetail['response'][0]['last_name']; |
||
| 118 | } |
||
| 119 | if (!$user->info->city && !empty($userDetail['response'][0]['home_town'])) { |
||
| 120 | $user->info->city = $userDetail['response'][0]['home_town']; |
||
| 121 | } |
||
| 122 | if (!$user->info->sex && !empty($userDetail['response'][0]['sex'])) { |
||
| 123 | $user->info->sex = $userDetail['response'][0]['sex'] == 2 ? 1 : ($userDetail['response'][0]['sex'] == 1 ? 2 : 0); |
||
| 124 | } |
||
| 125 | if ($user->info->bday == '0000-00-00' && !empty($userDetail['response'][0]['bdate'])) { |
||
| 126 | $user->info->bday = substr_count($userDetail['response'][0]['bdate'], '.') == 2 ? \DateTime::createFromFormat('d.m.Y', $userDetail['response'][0]['bdate'])->format('Y-m-d') : (substr_count($userDetail['response'][0]['bdate'], '.') == 1 ? \DateTime::createFromFormat('d.m', $userDetail['response'][0]['bdate'])->format('Y-m-1') : '0000-00-00'); |
||
| 127 | } |
||
| 128 | $user->info->save(); |
||
| 129 | $userSocial = new \Users\User\Social(); |
||
| 130 | $userSocial->uid = $result['user_id']; |
||
| 131 | $userSocial->social_id = $social->id; |
||
| 132 | $userSocial->user_id = $user->id; |
||
| 133 | $userSocial->save(); |
||
| 134 | \App::$cur->users->newSession($user); |
||
| 135 | \Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type], 'Вы успешно зарегистрировались через ВКонтакте', 'success'); |
||
| 136 | } |
||
| 182 |