AccountUpdater::run()   C
last analyzed

Complexity

Conditions 8
Paths 224

Size

Total Lines 65
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 52
nc 224
nop 0
dl 0
loc 65
rs 6.8961
c 0
b 0
f 0

How to fix   Long Method   

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