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) { |
|
|
|
|
64
|
|
|
$manager = \Yii::createObject(TagManager::class); |
65
|
|
|
$manager->saveForMedia($media, $tags); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$usernames = (array)Text::getUsernames($media->caption); |
69
|
|
|
if ($usernames) { |
|
|
|
|
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
|
|
|
} |
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.