Passed
Push — master ( b2168e...f1daef )
by Paweł
03:26
created

AccountFullUpdate::run()   B

Complexity

Conditions 5
Paths 104

Size

Total Lines 55
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 41
nc 104
nop 0
dl 0
loc 55
rs 8.9262
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\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 Yii;
22
use yii\base\BaseObject;
23
use yii\web\NotFoundHttpException;
24
25
class AccountFullUpdate extends BaseObject implements ServiceInterface
26
{
27
    /**
28
     * @var \app\models\Account
29
     */
30
    public $account;
31
32
    public function run()
33
    {
34
        $proxyManager = Yii::createObject(ProxyManager::class);
35
        $accountUpdater = Yii::createObject([
36
            'class' => AccountUpdater::class,
37
            'account' => $this->account,
38
        ]);
39
40
        try {
41
            $proxy = $proxyManager->reserve($this->account);
42
            $httpClient = Client::factory($proxy, [], 3600);
43
44
            $scraper = Yii::createObject(AccountScraper::class, [
45
                $httpClient,
46
            ]);
47
48
            $accountData = $this->fetchAccountData($scraper);
49
            $posts = $scraper->fetchLastPosts($accountData->username);
50
51
            $proxyManager->release($proxy);
52
            unset($proxy);
53
54
55
            if ($accountData->isPrivate) {
56
                $accountUpdater
57
                    ->setIsInValid(AccountInvalidationType::IS_PRIVATE)
58
                    ->setNextStatsUpdate(true)
59
                    ->save();
60
            } else {
61
                $accountUpdater
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
            $accountUpdater
76
                ->setIsInValid(AccountInvalidationType::NOT_FOUND)
77
                ->setNextStatsUpdate(true)
78
                ->save();
79
        } catch (RequestException $exception) {
80
            $accountUpdater
81
                ->setIsInValid()
82
                ->setNextStatsUpdate(true)
83
                ->save();
84
        } finally {
85
            if (isset($proxy)) {
86
                $proxyManager->release($proxy);
87
            }
88
        }
89
90
    }
91
92
    private function fetchAccountData(AccountScraper $scraper): Account
93
    {
94
        $idents = array_filter([
95
            $this->account->username,
96
            $this->account->instagram_id,
97
        ]);
98
99
        foreach ($idents as $ident) {
100
            try {
101
                $accountData = $scraper->fetchOne($ident);
102
                if ($this->account->instagram_id && $accountData->id != $this->account->instagram_id) {
103
                    continue;
104
                }
105
            } catch (ClientException $exception) {
106
                Yii::error($exception->getMessage(), __METHOD__);
107
                continue;
108
            }
109
            break;
110
        }
111
112
        if (empty($accountData)) {
113
            throw new NotFoundHttpException();
114
        }
115
116
        return $accountData;
117
    }
118
119
}