|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Monitoring. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 21.06.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace app\components\builders; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use app\components\instagram\models\Tag; |
|
12
|
|
|
use app\components\traits\NextUpdateCalculatorTrait; |
|
13
|
|
|
use app\components\traits\SaveModelTrait; |
|
14
|
|
|
use app\components\traits\SetTagTrait; |
|
15
|
|
|
use app\models\TagStats; |
|
16
|
|
|
use yii\base\Component; |
|
17
|
|
|
|
|
18
|
|
|
class TagBuilder extends Component |
|
19
|
|
|
{ |
|
20
|
|
|
use SaveModelTrait, SetTagTrait, NextUpdateCalculatorTrait; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
public function init() |
|
23
|
|
|
{ |
|
24
|
|
|
parent::init(); |
|
25
|
|
|
$this->throwExceptionIfTagIsEmpty(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setIdents(Tag $tag) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->tag->name = $tag->name; |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setDetails(Tag $tag) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setIdents($tag); |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setMonitoring() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->tag->monitoring = 1; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function setIsValid() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->tag->is_valid = 1; |
|
|
|
|
|
|
52
|
|
|
$this->tag->invalidation_count = 0; |
|
53
|
|
|
$this->tag->invalidation_type_id = null; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setIsInvalid(?int $invalidationType = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->tag->is_valid = 0; |
|
|
|
|
|
|
61
|
|
|
$this->tag->invalidation_count = (int)$this->tag->invalidation_count + 1; |
|
62
|
|
|
$this->tag->invalidation_type_id = $invalidationType; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* If true, then will be automatically calculate from invalidation_count |
|
69
|
|
|
* |
|
70
|
|
|
* @param true|int|null $interval |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setNextStatsUpdate($interval = 24) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->tag->update_stats_after = $this->getNextUpdateDate($this->tag, $interval); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function setStats(Tag $tag, bool $createHistory = true) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->tag->touch('stats_updated_at'); |
|
|
|
|
|
|
83
|
|
|
if ($this->tag->media === null || $this->statsNeedUpdate($tag)) { |
|
84
|
|
|
$this->tag->media = $tag->media; |
|
85
|
|
|
$this->tag->likes = $tag->likes; |
|
86
|
|
|
$this->tag->min_likes = $tag->minLikes; |
|
87
|
|
|
$this->tag->max_likes = $tag->maxLikes; |
|
88
|
|
|
$this->tag->comments = $tag->comments; |
|
89
|
|
|
$this->tag->min_comments = $tag->minComments; |
|
90
|
|
|
$this->tag->max_comments = $tag->maxComments; |
|
91
|
|
|
|
|
92
|
|
|
if ($createHistory) { |
|
93
|
|
|
$this->createHistory(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function save() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->saveModel($this->tag); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function createHistory() |
|
106
|
|
|
{ |
|
107
|
|
|
$tagStats = new TagStats([ |
|
108
|
|
|
'tag_id' => $this->tag->id, |
|
109
|
|
|
'media' => $this->tag->media, |
|
110
|
|
|
'likes' => $this->tag->likes, |
|
111
|
|
|
'min_likes' => $this->tag->min_likes, |
|
112
|
|
|
'max_likes' => $this->tag->max_likes, |
|
113
|
|
|
'comments' => $this->tag->comments, |
|
114
|
|
|
'min_comments' => $this->tag->min_comments, |
|
115
|
|
|
'max_comments' => $this->tag->max_comments, |
|
116
|
|
|
]); |
|
117
|
|
|
$this->saveModel($tagStats); |
|
118
|
|
|
|
|
119
|
|
|
return $tagStats; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function statsNeedUpdate(Tag $tag) |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->tag->media != $tag->media || |
|
125
|
|
|
$this->tag->likes != $tag->likes || |
|
126
|
|
|
$this->tag->comments != $tag->comments || |
|
127
|
|
|
$this->tag->min_likes != $tag->minLikes || |
|
128
|
|
|
$this->tag->max_likes != $tag->maxLikes || |
|
129
|
|
|
$this->tag->min_comments != $tag->minComments || |
|
130
|
|
|
$this->tag->max_comments != $tag->maxComments; |
|
131
|
|
|
} |
|
132
|
|
|
} |