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

MediaManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B saveRelatedData() 0 22 6
A saveForAccount() 0 16 2
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 12.01.2018
6
 */
7
8
namespace app\components;
9
10
11
use app\components\traits\FindOrCreate;
12
use app\models\Account;
13
use app\models\AccountStats;
14
use app\models\Media;
15
use app\models\MediaAccount;
16
use app\models\MediaTag;
17
use app\models\Tag;
18
use jakim\ig\Text;
19
use Jakim\Model\Post;
20
use yii\base\Component;
21
use yii\base\Exception;
22
23
class MediaManager extends Component
24
{
25
    use FindOrCreate;
26
27
    /**
28
     * @var \app\models\Account
29
     */
30
    public $account;
31
32
    /**
33
     * @param \app\models\Account $account
34
     * @param \app\components\instagram\models\Post[]
35
     * @throws \yii\base\InvalidConfigException
36
     */
37
    public function saveForAccount(Account $account, array $posts)
38
    {
39
        foreach ($posts as $post) {
40
            /** @var Media $media */
41
            $media = $this->findOrCreate([
42
                'account_id' => $account->id,
43
                'shortcode' => $post->shortcode,
44
            ], Media::class);
45
            /** @var \app\components\MediaUpdater $updater */
46
            $updater = \Yii::createObject([
47
                'class' => MediaUpdater::class,
48
                'media' => $media,
49
            ]);
50
            $updater->details($post);
51
52
            $this->saveRelatedData($media, $account);
53
        }
54
    }
55
56
    protected function saveRelatedData(Media $media, Account $account)
57
    {
58
        if (empty($media->caption)) {
59
            return false;
60
        }
61
62
        $tags = (array)Text::getTags($media->caption);
63
        if ($tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
            $manager = \Yii::createObject(TagManager::class);
65
            $manager->saveForMedia($media, $tags);
66
        }
67
68
        $usernames = (array)Text::getUsernames($media->caption);
69
        if ($usernames) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $usernames of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70
            // ignore owner of media
71
            ArrayHelper::removeValue($usernames, $account->username);
72
73
            $manager = \Yii::createObject(AccountManager::class);
74
            $manager->saveForMedia($media, $usernames);
75
76
            if ($account && $account->accounts_monitoring_level > 0) {
77
                $manager->monitorRelatedAccounts($account, $usernames);
78
            }
79
        }
80
    }
81
}