TagBuilder::statsNeedUpdate()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 7
nc 7
nop 1
dl 0
loc 9
rs 8.8333
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The trait app\components\traits\SaveModelTrait requires the property $errors which is not provided by app\components\builders\TagBuilder.
Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
The property $is_valid was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
The property $is_valid was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $interval can also be of type true; however, parameter $interval of app\components\builders\...er::getNextUpdateDate() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $this->tag->update_stats_after = $this->getNextUpdateDate($this->tag, /** @scrutinizer ignore-type */ $interval);
Loading history...
76
77
        return $this;
78
    }
79
80
    public function setStats(Tag $tag, bool $createHistory = true)
81
    {
82
        $this->tag->touch('stats_updated_at');
0 ignored issues
show
Bug introduced by
The method touch() does not exist on app\models\Tag. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $this->tag->/** @scrutinizer ignore-call */ 
83
                    touch('stats_updated_at');
Loading history...
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
}