Passed
Pull Request — master (#129)
by
unknown
04:25
created

AccountFullUpdate::run()   C

Complexity

Conditions 10
Paths 292

Size

Total Lines 75
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 54
c 1
b 0
f 0
nc 292
nop 0
dl 0
loc 75
rs 5.5503

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
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 18.06.2018
6
 */
7
8
namespace app\components\services;
9
10
11
use app\components\http\Client;
12
use app\components\http\ProxyManager;
13
use app\components\instagram\AccountScraper;
14
use app\components\instagram\models\Account;
15
use app\components\MediaManager;
16
use app\components\services\contracts\ServiceInterface;
17
use app\components\updaters\AccountUpdater;
18
use app\dictionaries\AccountInvalidationType;
19
use GuzzleHttp\Exception\ClientException;
20
use GuzzleHttp\Exception\RequestException;
21
use Jakim\Exception\RestrictedProfileException;
22
use Yii;
23
use yii\base\BaseObject;
24
use yii\web\NotFoundHttpException;
25
26
class AccountFullUpdate extends BaseObject implements ServiceInterface
27
{
28
    /**
29
     * @var \app\models\Account
30
     */
31
    public $account;
32
33
    public function run()
34
    {
35
        /**
36
         * @var ProxyManager $proxyManager
37
         */
38
        $proxyManager = Yii::createObject(ProxyManager::class);
39
        /**
40
         * @var AccountUpdater $accountUpdater
41
         */
42
        $accountUpdater = Yii::createObject([
43
            'class' => AccountUpdater::class,
44
            'account' => $this->account,
45
        ]);
46
47
        $proxy = $proxyManager->reserve($this->account);
48
        try {
49
            $httpClient = Client::factory($proxy, [], 3600);
50
51
            $scraper = Yii::createObject(AccountScraper::class, [
52
                $httpClient,
53
            ]);
54
55
            $accountData = $this->fetchAccountData($scraper);
56
            $posts = $scraper->fetchLastPosts($accountData->username);
57
58
            $proxyManager->release($proxy);
59
            unset($proxy);
60
61
62
            if ($accountData->isPrivate) {
63
                $accountUpdater
64
                    ->setIsInValid(AccountInvalidationType::IS_PRIVATE)
65
                    ->setNextStatsUpdate(true)
66
                    ->save();
67
            } else {
68
                $accountUpdater
69
                    ->setDetails($accountData)
70
                    ->setIdents($accountData)
71
                    ->setIsValid()
72
                    ->setStats($accountData, $posts)
73
                    ->setNextStatsUpdate()
74
                    ->save();
75
76
                $mediaManager = Yii::createObject(MediaManager::class);
77
                $mediaManager->addToAccount($this->account, $posts);
78
79
            }
80
81
        } catch (NotFoundHttpException $exception) {
82
            $accountUpdater
83
                ->setIsInValid(AccountInvalidationType::NOT_FOUND)
84
                ->setNextStatsUpdate(true)
85
                ->save();
86
        } catch (RestrictedProfileException $exception) {
87
            $accountUpdater
88
                ->setIsInValid(AccountInvalidationType::RESTRICTED_PROFILE)
89
                ->setNextStatsUpdate(true)
90
                ->save();
91
        } catch (RequestException $exception) {
92
            if (isset($proxy) && (strpos($exception->getMessage(), "Failed to connect to " . $proxy->ip) !== false || strpos($exception->getMessage(), "malformed") !== false)) {
93
                $type = strpos($exception->getMessage(), "malformed") !== false ? AccountInvalidationType::PROXY_ERROR : AccountInvalidationType::PROXY_TIMEOUT;
94
                $proxyManager->invalidate($proxy);
95
                $accountUpdater
96
                    ->setIsInvalid($type)
97
                    ->setNextStatsUpdate(true)
98
                    ->save();
99
            } else {
100
                $accountUpdater
101
                    ->setIsInvalidUnknown($exception->getMessage())
102
                    ->setNextStatsUpdate(true)
103
                    ->save();
104
            }
105
        } finally {
106
            if (isset($proxy)) {
107
                $proxyManager->release($proxy);
108
            }
109
        }
110
111
    }
112
113
    private function fetchAccountData(AccountScraper $scraper): Account
114
    {
115
        $idents = array_filter([
116
            $this->account->username,
117
            $this->account->instagram_id,
118
        ]);
119
120
        foreach ($idents as $ident) {
121
            try {
122
                $accountData = $scraper->fetchOne($ident);
123
                if ($this->account->instagram_id && $accountData->id != $this->account->instagram_id) {
124
                    continue;
125
                }
126
            } catch (ClientException $exception) {
127
                Yii::error($exception->getMessage(), __METHOD__);
128
                continue;
129
            }
130
            break;
131
        }
132
133
        if (empty($accountData)) {
134
            throw new NotFoundHttpException();
135
        }
136
137
        return $accountData;
138
    }
139
140
}