Passed
Push — master ( 2b7a92...80fd4c )
by Paweł
03:04
created

AccountUpdate::execute()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 10
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 11.01.2018
6
 */
7
8
namespace app\jobs;
9
10
11
use app\components\AccountManager;
12
use app\components\instagram\AccountScraper;
13
use app\components\ProxyManager;
14
use app\models\Account;
15
use app\models\Proxy;
16
use yii\base\Exception;
17
use yii\queue\JobInterface;
18
use yii\queue\Queue;
19
20
class AccountUpdate implements JobInterface
21
{
22
    public $id;
23
24
    /**
25
     * @param \yii\queue\Queue $queue
26
     * @throws \yii\base\Exception
27
     * @throws \yii\base\InvalidConfigException
28
     * @throws \Throwable
29
     */
30
    public function execute($queue)
31
    {
32
        $account = Account::findOne($this->id);
33
        if ($account && !$account->disabled) {
34
35
            $proxyManager = \Yii::createObject(ProxyManager::class);
36
37
            try {
38
39
                $proxy = $proxyManager->reserve($account);
40
41
                $manager = \Yii::createObject([
42
                    'class' => AccountManager::class,
43
                    'scraper' => [
44
                        'class' => AccountScraper::class,
45
                        'proxy' => $proxy,
46
                    ],
47
                ]);
48
                $manager->update($account);
49
                $proxyManager->release($proxy);
50
51
            } catch (\Throwable $exception) {
52
                
53
                if (isset($proxy)) {
54
                    $proxyManager->release($proxy);
55
                }
56
57
                throw $exception;
58
            }
59
60
        }
61
    }
62
}