Passed
Push — main ( 0b35b5...f9c7b3 )
by Tan
02:29
created

LikeCount   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A countDigital() 0 11 3
A dislikesCountDigital() 0 3 1
A dislikesCount() 0 3 1
A likesCount() 0 3 1
A likesCountDigital() 0 3 1
1
<?php
2
3
namespace CSlant\LaravelLike\Traits\Like;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
8
/**
9
 * Trait LikeCount
10
 *
11
 * @package CSlant\LaravelLike\Traits\Like
12
 * @mixin Model
13
 *
14
 * @method MorphMany likesTo()
15
 * @method MorphMany dislikesTo()
16
 */
17
trait LikeCount
18
{
19
    /**
20
     * Get the count of likes.
21
     *
22
     * @return int
23
     */
24
    public function likesCount(): int
25
    {
26
        return $this->likesTo()->count();
27
    }
28
29
    /**
30
     * Get the count of dislikes.
31
     *
32
     * @return int
33
     */
34
    public function dislikesCount(): int
35
    {
36
        return $this->dislikesTo()->count();
37
    }
38
39
    /**
40
     * Get digital count.
41
     *
42
     * @param $count
43
     *
44
     * @return string
45
     */
46
    public function countDigital($count): string
47
    {
48
        if ($count < 1000) {
49
            return $count;
50
        }
51
52
        if ($count < 1000000) {
53
            return round($count / 1000, 1) . 'K';
54
        }
55
56
        return round($count / 1000000, 1) . 'M';
57
    }
58
59
    /**
60
     * Get the count of likes in digital format.
61
     *
62
     * @return string
63
     */
64
    public function likesCountDigital(): string
65
    {
66
        return $this->countDigital($this->likesCount());
67
    }
68
69
    /**
70
     * Get the count of dislikes in digital format.
71
     *
72
     * @return string
73
     */
74
    public function dislikesCountDigital(): string
75
    {
76
        return $this->countDigital($this->dislikesCount());
77
    }
78
}
79