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\FindOrCreateTrait; |
||
12 | use app\components\builders\MediaBuilder; |
||
13 | use app\models\Account; |
||
14 | use app\models\Media; |
||
15 | use jakim\ig\Text; |
||
16 | use Yii; |
||
17 | use yii\base\Component; |
||
18 | |||
19 | class MediaManager extends Component |
||
20 | { |
||
21 | use FindOrCreateTrait; |
||
22 | |||
23 | /** |
||
24 | * @var \app\models\Account |
||
25 | */ |
||
26 | public $account; |
||
27 | |||
28 | /** |
||
29 | * @param \app\models\Account $account |
||
30 | * @param \app\components\instagram\models\Post[] |
||
31 | * @throws \yii\base\InvalidConfigException |
||
32 | */ |
||
33 | public function addToAccount(Account $account, array $posts) |
||
34 | { |
||
35 | foreach ($posts as $post) { |
||
36 | /** @var Media $media */ |
||
37 | $media = $this->findOrCreate([ |
||
38 | 'account_id' => $account->id, |
||
39 | 'shortcode' => $post->shortcode, |
||
40 | ], Media::class); |
||
41 | /** @var \app\components\builders\MediaBuilder $updater */ |
||
42 | $updater = Yii::createObject([ |
||
43 | 'class' => MediaBuilder::class, |
||
44 | 'media' => $media, |
||
45 | ]); |
||
46 | $updater->setDetails($post) |
||
47 | ->save(); |
||
48 | |||
49 | $this->saveRelatedData($media, $account); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | protected function saveRelatedData(Media $media, Account $account) |
||
54 | { |
||
55 | if (empty($media->caption)) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | $tags = (array) Text::getTags($media->caption); |
||
60 | if ($tags) { |
||
0 ignored issues
–
show
|
|||
61 | $manager = Yii::createObject(TagManager::class); |
||
62 | $manager->addToMedia($media, $tags); |
||
63 | } |
||
64 | |||
65 | $usernames = (array) Text::getUsernames($media->caption); |
||
66 | if ($usernames) { |
||
0 ignored issues
–
show
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 ![]() |
|||
67 | // ignore owner of media |
||
68 | ArrayHelper::removeValue($usernames, $account->username); |
||
69 | |||
70 | $manager = Yii::createObject(AccountManager::class); |
||
71 | $manager->addToMedia($media, $usernames); |
||
72 | } |
||
73 | |||
74 | return true; |
||
75 | } |
||
76 | } |
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.