Completed
Push — master ( 5c516d...6e0e1d )
by Alexey
04:27
created

Twitter   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 182
Duplicated Lines 44.51 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 81
loc 182
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
B requestToken() 0 27 1
B verify() 36 36 1
B getInfo() 37 37 1
F auth() 8 75 21

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Twitter extends \Users\SocialHelper {
15
16
  private static function requestToken() {
17
    $config = static::getConfig();
18
    $oauthNonce = md5(uniqid(rand(), true));
19
    $oauthTimestamp = time();
20
    //string
21
    $oauth_base_text = "GET&";
22
    $oauth_base_text .= urlencode('https://api.twitter.com/oauth/request_token') . "&";
23
    $oauth_base_text .= urlencode("oauth_callback=" . urlencode('http://' . INJI_DOMAIN_NAME . '/users/social/auth/twitter') . "&");
24
    $oauth_base_text .= urlencode("oauth_consumer_key=" . $config['consumer_key'] . "&");
25
    $oauth_base_text .= urlencode("oauth_nonce=" . $oauthNonce . "&");
26
    $oauth_base_text .= urlencode("oauth_signature_method=HMAC-SHA1&");
27
    $oauth_base_text .= urlencode("oauth_timestamp=" . $oauthTimestamp . "&");
28
    $oauth_base_text .= urlencode("oauth_version=1.0");
29
    $oauthSignature = base64_encode(hash_hmac("sha1", $oauth_base_text, $config['consumer_secret'] . "&", true));
30
    //request
31
    $url = 'https://api.twitter.com/oauth/request_token';
32
    $url .= '?oauth_callback=' . urlencode('http://' . INJI_DOMAIN_NAME . '/users/social/auth/twitter');
33
    $url .= '&oauth_consumer_key=' . $config['consumer_key'];
34
    $url .= '&oauth_nonce=' . $oauthNonce;
35
    $url .= '&oauth_signature=' . urlencode($oauthSignature);
36
    $url .= '&oauth_signature_method=HMAC-SHA1';
37
    $url .= '&oauth_timestamp=' . $oauthTimestamp;
38
    $url .= '&oauth_version=1.0';
39
    $response = file_get_contents($url);
40
    parse_str($response, $result);
41
    return $result;
42
  }
43
44 View Code Duplication
  private static function verify() {
2 ignored issues
show
Coding Style introduced by
verify uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
verify uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
45
    $config = static::getConfig();
46
    $oauthNonce = md5(uniqid(rand(), true));
47
    $oauthTimestamp = time();
48
    $oauth_token = $_GET['oauth_token'];
49
    $oauth_verifier = $_GET['oauth_verifier'];
50
    $oauth_token_secret = $_SESSION['oauth_token_secret'];
51
    //string
52
    $oauth_base_text = "GET&";
53
    $oauth_base_text .= urlencode('https://api.twitter.com/oauth/access_token') . "&";
54
    $oauth_base_text .= urlencode("oauth_consumer_key=" . $config['consumer_key'] . "&");
55
    $oauth_base_text .= urlencode("oauth_nonce=" . $oauthNonce . "&");
56
    $oauth_base_text .= urlencode("oauth_signature_method=HMAC-SHA1&");
57
    $oauth_base_text .= urlencode("oauth_token=" . $oauth_token . "&");
58
    $oauth_base_text .= urlencode("oauth_timestamp=" . $oauthTimestamp . "&");
59
    $oauth_base_text .= urlencode("oauth_verifier=" . $oauth_verifier . "&");
60
    $oauth_base_text .= urlencode("oauth_version=1.0");
61
62
    $key = $config['consumer_secret'] . "&" . $oauth_token_secret;
63
    //request
64
    $oauth_signature = base64_encode(hash_hmac("sha1", $oauth_base_text, $key, true));
65
    $url = 'https://api.twitter.com/oauth/access_token';
66
    $url .= '?oauth_nonce=' . $oauthNonce;
67
    $url .= '&oauth_signature_method=HMAC-SHA1';
68
    $url .= '&oauth_timestamp=' . $oauthTimestamp;
69
    $url .= '&oauth_consumer_key=' . $config['consumer_key'];
70
    $url .= '&oauth_token=' . urlencode($oauth_token);
71
    $url .= '&oauth_verifier=' . urlencode($oauth_verifier);
72
    $url .= '&oauth_signature=' . urlencode($oauth_signature);
73
    $url .= '&oauth_version=1.0';
74
75
76
    $response = file_get_contents($url);
77
    parse_str($response, $result);
78
    return $result;
79
  }
80
81 View Code Duplication
  private static function getInfo($result) {
82
    $config = static::getConfig();
83
    $oauth_nonce = md5(uniqid(rand(), true));
84
    $oauth_timestamp = time();
85
86
    $oauth_token = $result['oauth_token'];
87
    $oauth_token_secret = $result['oauth_token_secret'];
88
    $screen_name = $result['screen_name'];
89
90
    $oauth_base_text = "GET&";
91
    $oauth_base_text .= urlencode('https://api.twitter.com/1.1/users/show.json') . '&';
92
    $oauth_base_text .= urlencode('oauth_consumer_key=' . $config['consumer_key'] . '&');
93
    $oauth_base_text .= urlencode('oauth_nonce=' . $oauth_nonce . '&');
94
    $oauth_base_text .= urlencode('oauth_signature_method=HMAC-SHA1&');
95
    $oauth_base_text .= urlencode('oauth_timestamp=' . $oauth_timestamp . "&");
96
    $oauth_base_text .= urlencode('oauth_token=' . $oauth_token . "&");
97
    $oauth_base_text .= urlencode('oauth_version=1.0&');
98
    $oauth_base_text .= urlencode('screen_name=' . $screen_name);
99
100
    $key = $config['consumer_secret'] . '&' . $oauth_token_secret;
101
    $signature = base64_encode(hash_hmac("sha1", $oauth_base_text, $key, true));
102
103
104
    $url = 'https://api.twitter.com/1.1/users/show.json';
105
    $url .= '?oauth_consumer_key=' . $config['consumer_key'];
106
    $url .= '&oauth_nonce=' . $oauth_nonce;
107
    $url .= '&oauth_signature=' . urlencode($signature);
108
    $url .= '&oauth_signature_method=HMAC-SHA1';
109
    $url .= '&oauth_timestamp=' . $oauth_timestamp;
110
    $url .= '&oauth_token=' . urlencode($oauth_token);
111
    $url .= '&oauth_version=1.0';
112
    $url .= '&screen_name=' . $screen_name;
113
114
    $response = file_get_contents($url);
115
116
    return json_decode($response, true);
117
  }
118
119
  public static function auth() {
4 ignored issues
show
Coding Style introduced by
auth uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
auth uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
auth uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
auth uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
120
    $config = static::getConfig();
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121
    if (empty($_GET['oauth_verifier']) || empty($_SESSION['oauth_token_secret'])) {
122
      $tokens = self::requestToken();
123
      $_SESSION['oauth_token_secret'] = $tokens['oauth_token_secret'];
124
      \Tools::redirect("https://api.twitter.com/oauth/authorize?oauth_token={$tokens['oauth_token']}");
125
    }
126
    $verify = static::verify();
0 ignored issues
show
Bug introduced by
Since verify() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of verify() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
127
128
    if (!$verify['user_id']) {
129
      \Tools::redirect('/', 'Не удалось авторизоваться через twitter');
130
    }
131
    $userDetail = static::getInfo($verify);
0 ignored issues
show
Bug introduced by
Since getInfo() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getInfo() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
132
133
    $social = static::getObject();
134
    $userSocial = \Users\User\Social::get([['uid', $userDetail['id']], ['social_id', $social->id]]);
135
    if ($userSocial && $userSocial->user) {
136
      \App::$cur->users->newSession($userSocial->user);
137 View Code Duplication
      if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
        \Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]);
139
      }
140
    } else {
141
      if ($userSocial && !$userSocial->user) {
142
        $userSocial->delete();
143
      }
144
      if (!\Users\User::$cur->id) {
145
        $user = new \Users\User();
146
        $user->group_id = 2;
147
        $user->role_id = 2;
148
        $invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : '')))));
149
        if (!empty($invite_code)) {
150
          $invite = \Users\User\Invite::get($invite_code, 'code');
151
          $inveiteError = false;
152
          if (!$invite) {
153
            Msg::add('Такой код пришлашения не найден', 'danger');
154
            $inveiteError = true;
155
          }
156
          if ($invite->limit && !($invite->limit - $invite->count)) {
157
            Msg::add('Лимит приглашений для данного кода исчерпан', 'danger');
158
            $inveiteError = true;
159
          }
160
          if (!$inveiteError) {
161
            $user->parent_id = $invite->user_id;
162
            $invite->count++;
163
            $invite->save();
164
          }
165
        }
166
        if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) {
167
          $user->parent_id = \App::$cur->Users->config['defaultPartner'];
168
        }
169
        $user->save();
170
        $userInfo = new \Users\User\Info();
171
        $userInfo->user_id = $user->id;
172
        $userInfo->save();
173
      } else {
174
        $user = \Users\User::$cur;
175
      }
176
      $name = explode(' ', $userDetail['name']);
177
      $user->info->first_name = $name[0];
178
      $user->info->last_name = $name[1];
179
      $user->info->city = $userDetail['location'];
180
      $user->info->save();
181
      $userSocial = new \Users\User\Social();
182
      $userSocial->uid = $userDetail['id'];
183
      $userSocial->social_id = $social->id;
184
      $userSocial->user_id = $user->id;
185
      $userSocial->save();
186
      \App::$cur->users->newSession($user);
187 View Code Duplication
      if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
        \Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type], 'Вы успешно зарегистрировались через Twitter', 'success');
189
      } else {
190
        \Tools::redirect('/users/cabinet/profile', 'Вы успешно зарегистрировались через Twitter', 'success');
191
      }
192
    }
193
  }
194
195
}
196