Passed
Push — develop ( 4cbaa0...09ba9e )
by Ngoding
05:32
created

gravatar_image()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 7
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
1
<?php
2
3
use Illuminate\Support\Carbon;
4
5
if (! function_exists('terbilang')) {
6
    /**
7
     * Return the given value into readable number.
8
     *
9
     * @param  mixed  $value
10
     * @return string
11
     */
12
    function terbilang($value): string
13
    {
14
        $result = value(function () use ($value) {
15
            $angka = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan', 'sepuluh', 'sebelas'];
16
17
            $number = abs($value);
18
19
            switch (true) {
20
                case $number < 12:
21
                    return ' ' . $angka[$number];
22
23
                case $number < 20:
24
                    return terbilang($number - 10) . ' belas';
25
26
                case $number < 100:
27
                    return terbilang($number / 10) . ' puluh ' . terbilang($number % 10);
28
29
                case $number < 200:
30
                    return 'seratus ' . terbilang($number - 100);
31
32
                case $number < 1000:
33
                    return terbilang($number / 100) . ' ratus ' . terbilang($number % 100);
34
35
                case $number < 2000:
36
                    return 'seribu ' . terbilang($number - 1000);
37
38
                case $number < 1000000:
39
                    return terbilang($number / 1000) . ' ribu ' . terbilang($number % 1000);
40
41
                case $number < 1000000000:
42
                    return terbilang($number / 1000000) . ' juta ' . terbilang($number % 1000000);
43
44
                case $number < 1000000000000:
45
                    return terbilang($number / 1000000000) . ' milyar ' . terbilang($number % 1000000000);
46
47
                case $number < 1000000000000000:
48
                    return terbilang($number / 1000000000000) . ' trilyun ' . terbilang($number % 1000000000000);
49
            }
50
        });
51
52
        return trim(($value < 0 ? 'minus ' : '') . $result);
0 ignored issues
show
Bug introduced by
Are you sure $result of type callable|mixed can be used in concatenation? ( Ignorable by Annotation )

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

52
        return trim(($value < 0 ? 'minus ' : '') . /** @scrutinizer ignore-type */ $result);
Loading history...
53
    }
54
}
55
56
if (! function_exists('greeting')) {
57
    /**
58
     * Return specific greeting based on the current hour.
59
     *
60
     * @param  \Illuminate\Support\Carbon|null  $date
61
     * @return string
62
     */
63
    function greeting(Carbon $date = null): string
64
    {
65 1
        $hour = ($date ?? Carbon::now())->format('H');
66
67
        switch (true) {
68 1
            case $hour < 12:
69 1
                return trans('Good Morning');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('Good Morning') could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
70
71
            case $hour < 15:
72
                return trans('Good Afternoon');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('Good Afternoon') could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
73
74
            case $hour < 18:
75
                return trans('Good Evening');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('Good Evening') could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
76
77
            default:
78
                return trans('Good Night');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('Good Night') could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
79
        }
80
    }
81
}
82
83
if (! function_exists('gravatar_image')) {
84
    /**
85
     * Return avatar image url using gravatar.
86
     *
87
     * @param  string|null  $email
88
     * @param  int  $size
89
     * @param  string  $extension
90
     * @return string
91
     */
92
    function gravatar_image(string $email = null, int $size = 30, string $extension = 'png'): string
93
    {
94 1
        if (is_null($email)) {
95
            return sprintf('https://www.gravatar.com/avatar?s=%s', $size);
96
        }
97
98 1
        return sprintf('https://www.gravatar.com/avatar/%s.%s?s=%s', md5($email), $extension, $size);
99
    }
100
}
101