Passed
Push — develop ( 2fff64...5f0aa7 )
by Septianata
04:06
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
use Propaganistas\LaravelPhone\PhoneNumber;
9
10
if (! function_exists('terbilang')) {
11
    /**
12
     * Return the given value into readable number.
13
     *
14
     * @param  mixed  $value
15
     * @return string
16
     */
17
    function terbilang($value): string
18
    {
19
        $result = value(function () use ($value) {
20
            $angka = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan', 'sepuluh', 'sebelas'];
21
22
            $number = abs($value);
23
24
            switch (true) {
25
                case $number < 12:
26
                    return ' ' . $angka[$number];
27
28
                case $number < 20:
29
                    return terbilang($number - 10) . ' belas';
30
31
                case $number < 100:
32
                    return terbilang($number / 10) . ' puluh ' . terbilang($number % 10);
33
34
                case $number < 200:
35
                    return 'seratus ' . terbilang($number - 100);
36
37
                case $number < 1000:
38
                    return terbilang($number / 100) . ' ratus ' . terbilang($number % 100);
39
40
                case $number < 2000:
41
                    return 'seribu ' . terbilang($number - 1000);
42
43
                case $number < 1000000:
44
                    return terbilang($number / 1000) . ' ribu ' . terbilang($number % 1000);
45
46
                case $number < 1000000000:
47
                    return terbilang($number / 1000000) . ' juta ' . terbilang($number % 1000000);
48
49
                case $number < 1000000000000:
50
                    return terbilang($number / 1000000000) . ' milyar ' . terbilang($number % 1000000000);
51
52
                case $number < 1000000000000000:
53
                    return terbilang($number / 1000000000000) . ' trilyun ' . terbilang($number % 1000000000000);
54
            }
55
        });
56
57
        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

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

132
            env('TELEGRAM_TOKEN'), /** @scrutinizer ignore-type */ $photoFilePath
Loading history...
133
        ));
134
135
        $filename = Str::random() . '.' . pathinfo($photoFilePath, PATHINFO_EXTENSION);
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> 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

135
        $filename = Str::random() . '.' . pathinfo(/** @scrutinizer ignore-type */ $photoFilePath, PATHINFO_EXTENSION);
Loading history...
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

135
        $filename = Str::random() . '.' . /** @scrutinizer ignore-type */ pathinfo($photoFilePath, PATHINFO_EXTENSION);
Loading history...
136
137
        Storage::put($path . '/' . $filename, $photo);
138
139
        return $filename;
140
    }
141
}
142
143
if (! function_exists('google_map_url')) {
144
    /**
145
     * Return google map url based on the given latitude and longitude.
146
     *
147
     * @param  float  $latitude
148
     * @param  float  $longitude
149
     * @param  string  $zoom
150
     * @return string
151
     */
152
    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...
153
    {
154
        return sprintf(
155
            'https://www.google.com/maps/@%s,%s,%s',
156
            $latitude, $longitude, $zoom
157
        );
158
    }
159
}
160