Passed
Push — develop ( 728d57...c3bbc8 )
by Septianata
05:08
created

greeting()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
ccs 5
cts 8
cp 0.625
rs 9.9332
cc 4
nc 4
nop 1
crap 4.8437
1
<?php
2
3
use Illuminate\Support\Arr;
4
use Illuminate\Support\Carbon;
5
use Illuminate\Support\Facades\Http;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Str;
8
9
if (! function_exists('terbilang')) {
10
    /**
11
     * Return the given value into readable number.
12
     *
13
     * @param  mixed  $value
14
     * @return string
15
     */
16
    function terbilang($value): string
17
    {
18 9
        $result = value(function () use ($value) {
19 9
            $angka = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan', 'sepuluh', 'sebelas'];
20
21 9
            $number = abs($value);
22
23
            switch (true) {
24 9
                case $number < 12:
25 9
                    return ' ' . $angka[$number];
26
27 9
                case $number < 20:
28
                    return terbilang($number - 10) . ' belas';
29
30 9
                case $number < 100:
31 4
                    return terbilang($number / 10) . ' puluh ' . terbilang($number % 10);
32
33 9
                case $number < 200:
34 4
                    return 'seratus ' . terbilang($number - 100);
35
36 9
                case $number < 1000:
37 6
                    return terbilang($number / 100) . ' ratus ' . terbilang($number % 100);
38
39 8
                case $number < 2000:
40 4
                    return 'seribu ' . terbilang($number - 1000);
41
42 6
                case $number < 1000000:
43 6
                    return terbilang($number / 1000) . ' ribu ' . terbilang($number % 1000);
44
45
                case $number < 1000000000:
46
                    return terbilang($number / 1000000) . ' juta ' . terbilang($number % 1000000);
47
48
                case $number < 1000000000000:
49
                    return terbilang($number / 1000000000) . ' milyar ' . terbilang($number % 1000000000);
50
51
                case $number < 1000000000000000:
52
                    return terbilang($number / 1000000000000) . ' trilyun ' . terbilang($number % 1000000000000);
53
            }
54 9
        });
55
56 9
        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

56
        return trim(($value < 0 ? 'minus ' : '') . /** @scrutinizer ignore-type */ $result);
Loading history...
57
    }
58
}
59
60
if (! function_exists('greeting')) {
61
    /**
62
     * Return specific greeting based on the current hour.
63
     *
64
     * @param  \Illuminate\Support\Carbon|null  $date
65
     * @return string
66
     */
67
    function greeting(Carbon $date = null): string
68
    {
69 1
        $hour = ($date ?? Carbon::now())->format('H');
70
71
        switch (true) {
72 1
            case $hour < 12:
73
                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...
74
75 1
            case $hour < 15:
76
                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...
77
78 1
            case $hour < 18:
79 1
                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...
80
81
            default:
82
                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...
83
        }
84
    }
85
}
86
87
if (! function_exists('telegram_url')) {
88
    /**
89
     * Return base telegram bot API url.
90
     *
91
     * @param  string  $method
92
     * @return string
93
     */
94
    function telegram_url(string $method): string
95
    {
96
        return sprintf('https://api.telegram.org/bot%s/%s', env('TELEGRAM_TOKEN'), $method);
97
    }
98
}
99
100
if (! function_exists('download_telegram_photo')) {
101
    /**
102
     * Download telegram photo based on the given photo list.
103
     *
104
     * @param  array  $photos (from $this->getBot()->getMessage()->getPayload())
105
     * @param  string  $path
106
     * @return string
107
     *
108
     * @throws \Illuminate\Http\Client\RequestException
109
     * @throws \InvalidArgumentException
110
     */
111
    function download_telegram_photo(array $photos, string $path)
112
    {
113
        $photos = Arr::sort($photos, fn ($photo) => $photo['file_size']);
114
115
        $photoId = Arr::last($photos)['file_id'];
116
117
        throw_unless($photoId, InvalidArgumentException::class, sprintf(
118
            'Telegram file_id is not found', $photoId
119
        ));
120
121
        $photoFilePath = Http::get(telegram_url('getFile'), [
122
            'file_id' => $photoId,
123
        ])->throw()->json('result.file_path', null);
124
125
        throw_unless($photoFilePath, InvalidArgumentException::class, sprintf(
126
            'Telegram file path for file_id %s is not found', $photoId
127
        ));
128
129
        $photo = file_get_contents(sprintf(
130
            'https://api.telegram.org/file/bot%s/%s',
131
            env('TELEGRAM_TOKEN'), $photoFilePath
0 ignored issues
show
Bug introduced by
It seems like $photoFilePath can also be of type array and array and array and array<mixed,array|mixed|null>; however, parameter $values of sprintf() does only seem to accept double|integer|string, 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

131
            env('TELEGRAM_TOKEN'), /** @scrutinizer ignore-type */ $photoFilePath
Loading history...
132
        ));
133
134
        $filename = Str::random() . '.' . pathinfo($photoFilePath, PATHINFO_EXTENSION);
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($photoFilePath, PATHINFO_EXTENSION) of type array|string 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

134
        $filename = Str::random() . '.' . /** @scrutinizer ignore-type */ pathinfo($photoFilePath, PATHINFO_EXTENSION);
Loading history...
Bug introduced by
It seems like $photoFilePath can also be of type array and array and array and array<mixed,array|mixed|null> and null; however, parameter $path of pathinfo() does only seem to accept string, 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

134
        $filename = Str::random() . '.' . pathinfo(/** @scrutinizer ignore-type */ $photoFilePath, PATHINFO_EXTENSION);
Loading history...
135
136
        Storage::put($path . '/' . $filename, $photo);
137
138
        return $filename;
139
    }
140
}
141
142
if (! function_exists('google_map_url')) {
143
    /**
144
     * Return google map url based on the given latitude and longitude.
145
     *
146
     * @param  float  $latitude
147
     * @param  float  $longitude
148
     * @param  string  $zoom
149
     * @return string
150
     */
151
    function google_map_url(float $latitude, float $longitude, string $zoom = '20z'): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
    {
153 69
        return sprintf(
154 69
            'https://maps.google.com/?q=%s,%s&z=%s',
155
            // 'https://www.google.com/maps/@%s,%s,%s',
156 69
            $latitude, $longitude, $zoom
157
        );
158
    }
159
}
160
161
if (! function_exists('format_rupiah')) {
162
    /**
163
     * Return number in rupiah format.
164
     *
165
     * @param  float  $value
166
     * @param  string  $prefix
167
     * @return string
168
     */
169
    function format_rupiah(float $value, string $prefix = 'Rp'): string
170
    {
171
        return $prefix . number_format($value, 0, ',', '.');
172
    }
173
}
174