Passed
Branch feature/super-model (24c950)
by axel
02:55
created

Helper::response()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Helper;
4
5
/**
6
 *	Helper class.
7
 */
8
class Helper
9
{
10
	/**
11
     * Convert return result into easy-to-read result.
12
     *
13
     * @param string|array $response
14
     *
15
     * @return string|array
16
     */
17
    public static function toResponse($response)
18
    {
19
        switch ($response) {
20
            case 400:
21
                return 'Search query needs at least 3 letters';
22
            case 403:
23
                return 'Private user list';
24
            case 404:
25
                return 'Page not found';
26
            default:
27
                return $response;
28
        }
29
    }
30
31
    /**
32
     * Convert return result into http response.
33
     *
34
     * @param string|array $response
35
     *
36
     * @return string
37
     */
38
    public function response($response)
39
    {
40
        $result = [];
41
        if (is_numeric($response)) {
42
            header('HTTP/1.1 '.$response);
43
            $result['status'] = $response;
44
            $result['status_message'] = self::toResponse($response);
45
            $result['data'] = [];
46
        } else {
47
            header('HTTP/1.1 '. 200);
48
            $result['status'] = 200;
49
            $result['status_message'] = 'Success';
50
            $result['data'] = self::superEncode($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type string; however, parameter $array of MalScraper\Helper\Helper::superEncode() does only seem to accept array, 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

50
            $result['data'] = self::superEncode(/** @scrutinizer ignore-type */ $response);
Loading history...
Bug Best Practice introduced by
The method MalScraper\Helper\Helper::superEncode() is not static, but was called statically. ( Ignorable by Annotation )

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

50
            /** @scrutinizer ignore-call */ 
51
            $result['data'] = self::superEncode($response);
Loading history...
51
        }
52
53
        $json_response = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
54
        $json_response = str_replace('\\\\', '', $json_response);
55
56
        return $json_response;
57
    }
58
59
    /**
60
     * Convert characters to UTF-8.
61
     *
62
     * @param array $array
63
     *
64
     * @return array
65
     */
66
    private function superEncode($array)
67
    {
68
        if (is_array($array) && !empty($array)) {
69
            foreach ($array as $key => $value) {
70
                if (is_array($value)) {
71
                    $array[$key] = self::superEncode($value);
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Helper\Helper::superEncode() is not static, but was called statically. ( Ignorable by Annotation )

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

71
                    /** @scrutinizer ignore-call */ 
72
                    $array[$key] = self::superEncode($value);
Loading history...
72
                } else {
73
                    $array[$key] = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
74
                }
75
            }
76
        }
77
78
        return $array;
79
    }
80
81
    /**
82
     * Get top anime code.
83
     *
84
     * @param string|int $type
85
     *
86
     * @return string
87
     */
88
	public static function getTopAnimeType($type)
89
	{
90
	    $converted_type = '';
91
	    switch ($type) {
92
	        case '0':
93
	            $converted_type = '';
94
	            break;
95
	        case '1':
96
	            $converted_type = 'airing';
97
	            break;
98
	        case '2':
99
	            $converted_type = 'upcoming';
100
	            break;
101
	        case '3':
102
	            $converted_type = 'tv';
103
	            break;
104
	        case '4':
105
	            $converted_type = 'movie';
106
	            break;
107
	        case '5':
108
	            $converted_type = 'ova';
109
	            break;
110
	        case '6':
111
	            $converted_type = 'special';
112
	            break;
113
	        case '7':
114
	            $converted_type = 'bypopularity';
115
	            break;
116
	        case '8':
117
	            $converted_type = 'favorite';
118
	            break;
119
	        default:
120
	            $converted_type = '';
121
	    }
122
123
	    return $converted_type;
124
	}
125
126
	 /**
127
     * Get top manga code.
128
     *
129
     * @param string|int $type
130
     *
131
     * @return string
132
     */
133
	public static function getTopMangaType($type)
134
	{
135
	    $converted_type = '';
136
	    switch ($type) {
137
	        case '0':
138
	            $converted_type = '';
139
	            break;
140
	        case '1':
141
	            $converted_type = 'manga';
142
	            break;
143
	        case '2':
144
	            $converted_type = 'novels';
145
	            break;
146
	        case '3':
147
	            $converted_type = 'oneshots';
148
	            break;
149
	        case '4':
150
	            $converted_type = 'doujin';
151
	            break;
152
	        case '5':
153
	            $converted_type = 'manhwa';
154
	            break;
155
	        case '6':
156
	            $converted_type = 'manhua';
157
	            break;
158
	        case '7':
159
	            $converted_type = 'bypopularity';
160
	            break;
161
	        case '8':
162
	            $converted_type = 'favorite';
163
	            break;
164
	        default:
165
	            $converted_type = '';
166
	    }
167
168
	    return $converted_type;
169
	}
170
171
	 /**
172
     * Get current season.
173
     *
174
     * @return string
175
     */
176
	public static function getCurrentSeason()
177
	{
178
	    $day = new DateTime();
0 ignored issues
show
Bug introduced by
The type MalScraper\Helper\DateTime was not found. Did you mean DateTime? If so, make sure to prefix the type with \.
Loading history...
179
180
	    //  Days of spring
181
	    $spring_starts = new DateTime('April 1');
182
	    $spring_ends = new DateTime('June 30');
183
184
	    //  Days of summer
185
	    $summer_starts = new DateTime('July 1');
186
	    $summer_ends = new DateTime('September 30');
187
188
	    //  Days of autumn
189
	    $autumn_starts = new DateTime('October 1');
190
	    $autumn_ends = new DateTime('December 31');
191
192
	    //  If $day is between the days of spring, summer, autumn, and winter
193
	    if ($day >= $spring_starts && $day <= $spring_ends) :
194
	        $season = 'spring'; elseif ($day >= $summer_starts && $day <= $summer_ends) :
195
	        $season = 'summer'; elseif ($day >= $autumn_starts && $day <= $autumn_ends) :
196
	        $season = 'fall'; else :
197
	        $season = 'winter';
198
	    endif;
199
200
	    return $season;
201
	}
202
203
	 /**
204
     * Clean image URL.
205
     *
206
     * @param string $str
207
     *
208
     * @return string
209
     */
210
	public static function imageUrlCleaner($str)
211
	{
212
	    preg_match('/(questionmark)|(qm_50)/', $str, $temp_image);
213
	    $str = $temp_image ? '' : $str;
214
	    $str = str_replace('v.jpg', '.jpg', $str);
215
	    $str = str_replace('_thumb.jpg', '.jpg', $str);
216
	    $str = str_replace('userimages/thumbs', 'userimages', $str);
217
	    $str = preg_replace('/r\/\d{1,3}x\d{1,3}\//', '', $str);
218
	    $str = preg_replace('/\?.+/', '', $str);
219
220
	    return $str;
221
	}
222
}