Completed
Push — master ( 80fd4c...ece9a3 )
by Paweł
03:01
created

MediaManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B update() 0 31 4
A updateTags() 0 21 1
B updateUsernames() 0 24 3
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\instagram\AccountScraper;
12
use app\models\Account;
13
use app\models\Media;
14
use app\models\MediaAccount;
15
use app\models\MediaStats;
0 ignored issues
show
Bug introduced by
The type app\models\MediaStats was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    /**
26
     * @var \app\models\Account
27
     */
28
    public $account;
29
30
    /**
31
     * @param \app\models\Media $media
32
     * @param \Jakim\Model\Post $data
33
     * @throws \yii\base\Exception
34
     * @throws \yii\base\InvalidConfigException
35
     * @throws \yii\db\Exception
36
     */
37
    public function update(Media $media, Post $data)
38
    {
39
        $this->account = $media->account ?? $this->account;
40
41
        $media->instagram_id = $data->id;
42
        $media->shortcode = $data->shortcode;
43
        $media->is_video = $data->isVideo;
44
        $media->caption = $data->caption;
45
        $media->taken_at = (new \DateTime('@' . $data->takenAt))->format('Y-m-d H:i:s');
46
47
        $media->likes = $data->likes;
48
        $media->comments = $data->comments;
49
        if ($this->account->lastAccountStats) {
50
            $media->account_followed_by = $this->account->lastAccountStats->followed_by;
51
            $media->account_follows = $this->account->lastAccountStats->follows;
52
        }
53
54
        $media->account_id = $this->account->id;
55
56
        if (!$media->save()) {
57
            throw new Exception(json_encode($media->errors));
58
        }
59
60
        if ($media->caption) {
61
            $tags = (array)Text::getTags($media->caption);
62
            $this->updateTags($media, $tags);
63
64
            $usernames = (array)Text::getUsernames($media->caption);
65
            // ignore owner of media
66
            ArrayHelper::removeValue($usernames, $this->account->username);
67
            $this->updateUsernames($media, $usernames);
68
        }
69
    }
70
71
    public function updateUsernames(Media $media, array $usernames)
72
    {
73
        $manager = \Yii::createObject(AccountManager::class);
74
        $manager->saveUsernames($usernames);
75
        if ($this->account && $this->account->accounts_monitoring_level) {
76
            $manager->monitorMultiple($usernames, $this->account);
77
        }
78
79
        $createdAt = (new \DateTime())->format('Y-m-d H:i:s');
80
        $rows = array_map(function ($id) use ($media, $createdAt) {
81
            return [
82
                $media->id,
83
                $id,
84
                $createdAt,
85
            ];
86
        }, Account::find()
87
            ->andWhere(['username' => $usernames])
88
            ->column());
89
90
        $sql = \Yii::$app->db->queryBuilder
91
            ->batchInsert(MediaAccount::tableName(), ['media_id', 'account_id', 'created_at'], $rows);
92
        $sql = str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql);
93
        \Yii::$app->db->createCommand($sql)
94
            ->execute();
95
    }
96
97
    /**
98
     * @param \app\models\Media $media
99
     * @param array $tags
100
     * @throws \yii\base\InvalidConfigException
101
     * @throws \yii\db\Exception
102
     */
103
    public function updateTags(Media $media, array $tags)
104
    {
105
        $manager = \Yii::createObject(TagManager::class);
106
        $manager->saveTags($tags);
107
108
        $createdAt = (new \DateTime())->format('Y-m-d H:i:s');
109
        $rows = array_map(function ($id) use ($media, $createdAt) {
110
            return [
111
                $media->id,
112
                $id,
113
                $createdAt,
114
            ];
115
        }, Tag::find()
116
            ->andWhere(['name' => $tags])
117
            ->column());
118
119
        $sql = \Yii::$app->db->queryBuilder
120
            ->batchInsert(MediaTag::tableName(), ['media_id', 'tag_id', 'created_at'], $rows);
121
        $sql = str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql);
122
        \Yii::$app->db->createCommand($sql)
123
            ->execute();
124
    }
125
126
}