Passed
Push — master ( 6b49ff...b93f48 )
by Paweł
05:24
created

AccountFullUpdate::fetchAccountData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
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\AccountUpdater;
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 GuzzleHttp\Exception\ClientException;
19
use yii\base\BaseObject;
20
use yii\web\NotFoundHttpException;
21
22
class AccountFullUpdate extends BaseObject implements ServiceInterface
23
{
24
    /**
25
     * @var \app\models\Account
26
     */
27
    public $account;
28
29
    public function run()
30
    {
31
        $proxyManager = \Yii::createObject(ProxyManager::class);
32
33
        try {
34
            $proxy = $proxyManager->reserve($this->account);
35
            $httpClient = Client::factory($proxy, [], 3600);
36
37
            $scraper = \Yii::createObject(AccountScraper::class, [
38
                $httpClient,
39
            ]);
40
41
            $accountData = $this->fetchAccountData($scraper);
42
            $posts = $scraper->fetchLastPosts($accountData->username);
43
44
            $proxyManager->release($proxy);
45
            unset($proxy);
46
47
            $this->updateAccount($accountData, $posts);
48
49
        } catch (NotFoundHttpException $exception) {
50
            $this->account->disabled = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $disabled was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
51
            $this->account->save();
52
        } finally {
53
            if (isset($proxy)) {
54
                $proxyManager->release($proxy);
55
            }
56
        }
57
58
    }
59
60
    private function fetchAccountData(AccountScraper $scraper): Account
61
    {
62
        $idents = array_filter([
63
            $this->account->username,
64
            $this->account->instagram_id,
65
        ]);
66
67
        foreach ($idents as $ident) {
68
            try {
69
                $accountData = $scraper->fetchOne($ident);
70
            } catch (ClientException $exception) {
71
                \Yii::error($exception->getMessage(), __METHOD__);
72
                continue;
73
            };
74
            break;
75
        }
76
77
        if (empty($accountData)) {
78
            throw new NotFoundHttpException();
79
        }
80
81
        return $accountData;
82
    }
83
84
    private function updateAccount($accountData, $posts): void
85
    {
86
        $updater = \Yii::createObject([
87
            'class' => AccountUpdater::class,
88
            'account' => $this->account,
89
        ]);
90
91
        $updater->details($accountData);
92
93
        $stats = $updater->stats($accountData);
94
        $updater->er($stats, $posts);
95
96
        $mediaManager = \Yii::createObject(MediaManager::class);
97
        $mediaManager->saveForAccount($this->account, $posts);
98
    }
99
}