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

CategorizationPipeline::getCategorizers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Services\Categorization;
4
5
use App\Models\Settings;
6
use App\Models\UsenetGroup;
7
use App\Services\Categorization\Pipes\AbstractCategorizationPipe;
8
use App\Services\Categorization\Pipes\CategorizationPassable;
9
use Illuminate\Pipeline\Pipeline;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * Pipeline-based categorization service using Laravel Pipeline.
14
 *
15
 * This service uses Laravel's Pipeline to orchestrate multiple categorizers
16
 * to determine the best category for a release. Each categorizer (pipe) is
17
 * responsible for a specific category domain and returns a result with a
18
 * confidence score.
19
 */
20
class CategorizationPipeline
21
{
22
    /**
23
     * @var Collection<AbstractCategorizationPipe>
24
     */
25
    protected Collection $pipes;
26
27
    protected bool $categorizeForeign;
28
    protected bool $catWebDL;
29
30
    /**
31
     * @param iterable<AbstractCategorizationPipe> $pipes
32
     */
33
    public function __construct(iterable $pipes = [])
34
    {
35
        $this->pipes = collect($pipes)
0 ignored issues
show
Bug introduced by
It seems like $pipes can also be of type array; however, parameter $value of collect() does only seem to accept Illuminate\Contracts\Support\Arrayable, 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

35
        $this->pipes = collect(/** @scrutinizer ignore-type */ $pipes)
Loading history...
36
            ->sortBy(fn (AbstractCategorizationPipe $p) => $p->getPriority());
37
38
        $this->categorizeForeign = (bool) Settings::settingValue('categorizeforeign');
39
        $this->catWebDL = (bool) Settings::settingValue('catwebdl');
40
    }
41
42
    /**
43
     * Register a categorizer pipe in the pipeline.
44
     */
45
    public function addCategorizer(AbstractCategorizationPipe $pipe): self
46
    {
47
        $this->pipes->push($pipe);
48
        $this->pipes = $this->pipes->sortBy(fn (AbstractCategorizationPipe $p) => $p->getPriority());
49
50
        return $this;
51
    }
52
53
    /**
54
     * Determine the category for a release using Laravel Pipeline.
55
     *
56
     * @param int|string $groupId The usenet group ID
57
     * @param string $releaseName The name of the release
58
     * @param string|null $poster The poster name
59
     * @param bool $debug Whether to include debug information
60
     * @return array The categorization result
61
     */
62
    public function categorize(
63
        int|string $groupId,
64
        string $releaseName,
65
        ?string $poster = '',
66
        bool $debug = false
67
    ): array {
68
        $groupName = UsenetGroup::whereId($groupId)->value('name') ?? '';
69
70
        $context = new ReleaseContext(
71
            releaseName: $releaseName,
72
            groupId: $groupId,
73
            groupName: $groupName,
74
            poster: $poster ?? '',
75
            categorizeForeign: $this->categorizeForeign,
76
            catWebDL: $this->catWebDL,
77
        );
78
79
        $passable = new CategorizationPassable($context, $debug);
80
81
        /** @var CategorizationPassable $result */
82
        $result = app(Pipeline::class)
83
            ->send($passable)
84
            ->through($this->pipes->values()->all())
85
            ->thenReturn();
86
87
        return $result->toArray();
88
    }
89
90
    /**
91
     * Get all registered categorizers (pipes).
92
     *
93
     * @return Collection<AbstractCategorizationPipe>
94
     */
95
    public function getCategorizers(): Collection
96
    {
97
        return $this->pipes;
98
    }
99
100
    /**
101
     * Create a default pipeline with all standard categorizers.
102
     */
103
    public static function createDefault(): self
104
    {
105
        return new self([
106
            new Pipes\GroupNamePipe(),
107
            new Pipes\XxxPipe(),
108
            new Pipes\TvPipe(),
109
            new Pipes\MoviePipe(),
110
            new Pipes\BookPipe(),
111
            new Pipes\MusicPipe(),
112
            new Pipes\PcPipe(),
113
            new Pipes\ConsolePipe(),
114
            new Pipes\MiscPipe(),
115
        ]);
116
    }
117
}
118
119