Passed
Push — master ( 179943...1193c4 )
by Darko
09:13
created

ParseInfoPipe   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 22
c 1
b 0
f 0
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 26 4
A getLocalDb() 0 6 2
A getName() 0 3 1
A process() 0 4 1
A getStatusCode() 0 3 1
1
<?php
2
3
namespace App\Services\TvProcessing\Pipes;
4
5
use App\Services\TvProcessing\TvProcessingPassable;
6
use App\Services\TvProcessing\TvProcessingResult;
7
use Blacklight\processing\tv\LocalDB;
8
use Closure;
9
10
/**
11
 * Initial pipe that parses the release name into structured info.
12
 * This must run before any provider pipes.
13
 */
14
class ParseInfoPipe extends AbstractTvProviderPipe
15
{
16
    protected int $priority = 1;
17
    private ?LocalDB $localDb = null;
18
19
    public function getName(): string
20
    {
21
        return 'ParseInfo';
22
    }
23
24
    public function getStatusCode(): int
25
    {
26
        return 0;
27
    }
28
29
    /**
30
     * Get or create the LocalDB instance for parsing.
31
     */
32
    private function getLocalDb(): LocalDB
33
    {
34
        if ($this->localDb === null) {
35
            $this->localDb = new LocalDB();
36
        }
37
        return $this->localDb;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->localDb could return the type null which is incompatible with the type-hinted return Blacklight\processing\tv\LocalDB. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
40
    /**
41
     * Override handle to perform parsing before the standard processing flow.
42
     */
43
    public function handle(TvProcessingPassable $passable, Closure $next): TvProcessingPassable
44
    {
45
        $parsedInfo = $this->getLocalDb()->parseInfo($passable->context->searchName);
46
47
        if ($parsedInfo === false || empty($parsedInfo['name'])) {
48
            // Mark as parse failed
49
            $passable->setParsedInfo(null);
50
            $passable->updateResult(
51
                TvProcessingResult::parseFailed(['search_name' => $passable->context->searchName]),
52
                $this->getName()
53
            );
54
55
            if ($this->echoOutput) {
56
                $this->colorCli->error(sprintf(
57
                    '  ✗ Parse failed: %s',
58
                    mb_substr($passable->context->searchName, 0, 50)
59
                ));
60
            }
61
62
            // Don't continue to other pipes - can't process without parsed info
63
            return $passable;
64
        }
65
66
        $passable->setParsedInfo($parsedInfo);
0 ignored issues
show
Bug introduced by
It seems like $parsedInfo can also be of type true; however, parameter $info of App\Services\TvProcessin...ssable::setParsedInfo() does only seem to accept array|null, 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

66
        $passable->setParsedInfo(/** @scrutinizer ignore-type */ $parsedInfo);
Loading history...
67
68
        return $next($passable);
69
    }
70
71
    protected function process(TvProcessingPassable $passable): TvProcessingResult
72
    {
73
        // Not used - we override handle() instead
74
        return TvProcessingResult::pending();
75
    }
76
}
77