Profile::getProfile()   D
last analyzed

Complexity

Conditions 10
Paths 520

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 520
nop 1
dl 0
loc 61
rs 4.1842
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Integrations\Connectors\Instagram;
4
5
use Log;
6
use App\Models\User;
7
use Exception;
8
use Muleta\Utils\Debugger\ErrorHelper;
9
10
class Profile extends Instagram
11
{
12
    
13
    public function __construct()
14
    {
15
        
16
    }
17
18
    public static function getProfile($username)
19
    {
20
        try {
21
            $options  = array('http' => array('user_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)'));
22
            $context  = stream_context_create($options);
23
24
            $html = file_get_contents('https://instagram.com/'.$username, false, $context);
25
            //Get user ID
26
            $subData = substr($html, strpos($html, 'window._sharedData'), strpos($html, '};'));
27
            $userID = strstr($subData, '"id":"');
28
            $userID = str_replace('"id":"', '', $userID);
29
            $userID = strstr($userID, '"', true);
30
31
            //Download user info
32
            $jsonData = file_get_contents('https://i.instagram.com/api/v1/users/'.$userID.'/info/', false, $context);
33
            $decodedInfo = json_decode($jsonData);
34
35
            $data = [];
36
37
            if (isset($decodedInfo->user->hd_profile_pic_url_info->url)) {
38
                $data['profilePic'] = $decodedInfo->user->hd_profile_pic_url_info->url;
39
            } else {
40
                $data['profilePic'] = $decodedInfo->user->profile_pic_url;
41
            }
42
43
            if (isset($decodedInfo->user->username)) {
44
                $data['username'] = $decodedInfo->user->username;
45
            } 
46
47
            if (isset($decodedInfo->user->follower_count)) {
48
                $data['follower'] = $decodedInfo->user->follower_count;
49
            } 
50
51
            if (isset($decodedInfo->user->following_count)) {
52
                $data['following'] = $decodedInfo->user->following_count;
53
            } 
54
55
            if (isset($decodedInfo->user->full_name)) {
56
                $data['full_name'] = $decodedInfo->user->full_name;
57
            } 
58
59
            if (isset($decodedInfo->user->is_private)) {
60
                $data['isPrivate'] = $decodedInfo->user->is_private;
61
            } 
62
63
            if (isset($decodedInfo->user->is_verified)) {
64
                $data['isVerified'] = $decodedInfo->user->is_verified;
65
            } 
66
67
            if (isset($decodedInfo->user->biography)) {
68
                $data['bio'] = $decodedInfo->user->biography;
69
            }
70
            
71
            return $data;
72
            
73
        } catch (\Exception $e) {
74
            ErrorHelper::registerAndReturnMessage($e);
75
        }
76
        return false;
77
78
    }
79
80
}
81