Passed
Push — feature/super-model ( 9eaa75...66e03d )
by axel
01:46
created

Helper::imageUrlCleaner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 10
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
     * @return string
85
     */
86
    public static function getTopAnimeType()
87
    {
88
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('', 'airing...opularity', 'favorite') returns the type array<integer,string> which is incompatible with the documented return type string.
Loading history...
89
            '',
90
            'airing',
91
            'upcoming',
92
            'tv',
93
            'movie',
94
            'ova',
95
            'special',
96
            'bypopularity',
97
            'favorite',
98
        ];
99
    }
100
101
    /**
102
     * Get top manga code.
103
     *
104
     * @return string
105
     */
106
    public static function getTopMangaType()
107
    {
108
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('', 'manga'...opularity', 'favorite') returns the type array<integer,string> which is incompatible with the documented return type string.
Loading history...
109
            '',
110
            'manga',
111
            'novel',
112
            'oneshots',
113
            'doujin',
114
            'manhwa',
115
            'manhua',
116
            'bypopularity',
117
            'favorite',
118
        ];
119
    }
120
121
    /**
122
     * Get current season.
123
     *
124
     * @return string
125
     */
126
    public static function getCurrentSeason()
127
    {
128
        $currentMonth = date('m');
129
130
        if ($currentMonth >= '01' && $currentMonth < '04') return 'winter';
131
        if ($currentMonth >= '04' && $currentMonth < '07') return 'spring';
132
        if ($currentMonth >= '07' && $currentMonth < '10') return 'summer';
133
        return 'autumn';
134
    }
135
136
    /**
137
     * Clean image URL.
138
     *
139
     * @param string $str
140
     *
141
     * @return string
142
     */
143
    public static function imageUrlCleaner($str)
144
    {
145
        preg_match('/(questionmark)|(qm_50)/', $str, $temp_image);
146
        $str = $temp_image ? '' : $str;
147
        $str = str_replace('v.jpg', '.jpg', $str);
148
        $str = str_replace('_thumb.jpg', '.jpg', $str);
149
        $str = str_replace('userimages/thumbs', 'userimages', $str);
150
        $str = preg_replace('/r\/\d{1,3}x\d{1,3}\//', '', $str);
151
        $str = preg_replace('/\?.+/', '', $str);
152
153
        return $str;
154
    }
155
}
156