Passed
Push — master ( 03c468...179943 )
by Darko
11:56
created

ConsoleCategorizer::categorize()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 16
rs 6.6166
cc 13
nc 13
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\Services\Categorization\Categorizers;
3
use App\Models\Category;
4
use App\Services\Categorization\CategorizationResult;
5
use App\Services\Categorization\ReleaseContext;
6
class ConsoleCategorizer extends AbstractCategorizer
7
{
8
    protected int $priority = 35;
9
    public function getName(): string { return 'Console'; }
10
    public function shouldSkip(ReleaseContext $context): bool {
11
        if ($context->hasAdultMarkers()) return true;
12
        // Skip TV shows (season patterns)
13
        if (preg_match('/[._ -]S\d{1,3}[._ -]?(E\d|Complete|Full|1080|720|480|2160|WEB|HDTV|BluRay)/i', $context->releaseName)) return true;
14
        return false;
15
    }
16
    public function categorize(ReleaseContext $context): CategorizationResult
17
    {
18
        $name = $context->releaseName;
19
        if ($result = $this->checkPS4($name)) return $result;
20
        if ($result = $this->checkPS3($name)) return $result;
21
        if ($result = $this->checkPSVita($name)) return $result;
22
        if ($result = $this->checkPSP($name)) return $result;
23
        if ($result = $this->checkXboxOne($name)) return $result;
24
        if ($result = $this->checkXbox360($name)) return $result;
25
        if ($result = $this->checkXbox($name)) return $result;
26
        if ($result = $this->checkWiiU($name)) return $result;
27
        if ($result = $this->checkWii($name)) return $result;
28
        if ($result = $this->check3DS($name)) return $result;
29
        if ($result = $this->checkNDS($name)) return $result;
30
        if ($result = $this->checkOther($name)) return $result;
31
        return $this->noMatch();
32
    }
33
    protected function checkPS4(string $name): ?CategorizationResult
34
    {
35
        if (preg_match('/^PS4[_\.\-]/i', $name) || preg_match('/CUSA\d{5}/i', $name) ||
36
            preg_match('/\.PS4-DUPLEX$/i', $name) || preg_match('/\bPS4\b|PlayStation\s*4/i', $name)) {
37
            return $this->matched(Category::GAME_PS4, 0.9, 'ps4');
38
        }
39
        return null;
40
    }
41
    protected function checkPS3(string $name): ?CategorizationResult
42
    {
43
        if (preg_match('/\bPS3\b|PlayStation\s*3/i', $name)) return $this->matched(Category::GAME_PS3, 0.9, 'ps3');
44
        return null;
45
    }
46
    protected function checkPSVita(string $name): ?CategorizationResult
47
    {
48
        if (preg_match('/\bPS\s?Vita\b|PSV(ita)?\b/i', $name)) return $this->matched(Category::GAME_PSVITA, 0.9, 'psvita');
49
        return null;
50
    }
51
    protected function checkPSP(string $name): ?CategorizationResult
52
    {
53
        if (preg_match('/\bPSP\b|PlayStation\s*Portable/i', $name)) return $this->matched(Category::GAME_PSP, 0.9, 'psp');
54
        return null;
55
    }
56
    protected function checkXboxOne(string $name): ?CategorizationResult
57
    {
58
        if (preg_match('/\b(XboxOne|XBOX\s*One|XBONE|XB1|Xbox\s*Series[._ -]?[SX]|XSX|XSS)\b/i', $name)) {
59
            return $this->matched(Category::GAME_XBOXONE, 0.9, 'xboxone');
60
        }
61
        return null;
62
    }
63
    protected function checkXbox360(string $name): ?CategorizationResult
64
    {
65
        if (preg_match('/\b(Xbox360|XBOX360|X360)\b/i', $name)) return $this->matched(Category::GAME_XBOX360, 0.9, 'xbox360');
66
        return null;
67
    }
68
    protected function checkXbox(string $name): ?CategorizationResult
69
    {
70
        if (preg_match('/\bXBOX\b/i', $name) && !preg_match('/\b(XBOX\s?360|XBOX\s?ONE|Series)\b/i', $name)) {
71
            return $this->matched(Category::GAME_XBOX, 0.85, 'xbox');
72
        }
73
        return null;
74
    }
75
    protected function checkWiiU(string $name): ?CategorizationResult
76
    {
77
        if (preg_match('/\bWii\s*U\b|WiiU/i', $name)) return $this->matched(Category::GAME_WIIU, 0.9, 'wiiu');
78
        return null;
79
    }
80
    protected function checkWii(string $name): ?CategorizationResult
81
    {
82
        if (preg_match('/\bWii\b/i', $name) && !preg_match('/WiiU/i', $name)) return $this->matched(Category::GAME_WII, 0.85, 'wii');
83
        return null;
84
    }
85
    protected function check3DS(string $name): ?CategorizationResult
86
    {
87
        if (preg_match('/\b3DS\b|Nintendo\s*3DS/i', $name)) return $this->matched(Category::GAME_3DS, 0.9, '3ds');
88
        return null;
89
    }
90
    protected function checkNDS(string $name): ?CategorizationResult
91
    {
92
        if (preg_match('/\bNDS\b|Nintendo\s*DS/i', $name)) return $this->matched(Category::GAME_NDS, 0.9, 'nds');
93
        return null;
94
    }
95
    protected function checkOther(string $name): ?CategorizationResult
96
    {
97
        if (preg_match('/\b(PS[12X]|PS2|SNES|NES|SEGA|GB[AC]?|Dreamcast|Saturn|Atari|N64)\b/i', $name) &&
98
            preg_match('/\b(EUR|JP|JPN|NTSC|PAL|USA|ROM)\b/i', $name)) {
99
            return $this->matched(Category::GAME_OTHER, 0.8, 'retro_console');
100
        }
101
        return null;
102
    }
103
}
104