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

CategorizationService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 1
b 0
f 0
dl 0
loc 82
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addCategorizer() 0 4 1
A getPipeline() 0 3 1
A batchCategorize() 0 16 2
A determineCategory() 0 7 1
A getCategorizerStats() 0 11 1
1
<?php
2
3
namespace App\Services\Categorization;
4
5
use App\Models\Category;
6
7
/**
8
 * Categorization service using the new pipeline-based system.
9
 *
10
 * This class is a drop-in replacement for the legacy Blacklight\Categorize
11
 * with additional features like confidence scoring and debug information.
12
 */
13
class CategorizationService
14
{
15
    protected CategorizationPipeline $pipeline;
16
17
    public function __construct(?CategorizationPipeline $pipeline = null)
18
    {
19
        $this->pipeline = $pipeline ?? CategorizationPipeline::createDefault();
20
    }
21
22
    /**
23
     * Determine category for a release.
24
     *
25
     * @param int|string $groupId The usenet group ID
26
     * @param string $releaseName The name of the release
27
     * @param string|null $poster The poster name
28
     * @param bool $debug Whether to include debug information
29
     * @return array The categorization result with category ID and optional debug info
30
     */
31
    public function determineCategory(
32
        int|string $groupId,
33
        string $releaseName = '',
34
        ?string $poster = '',
35
        bool $debug = false
36
    ): array {
37
        return $this->pipeline->categorize($groupId, $releaseName, $poster, $debug);
38
    }
39
40
    /**
41
     * Batch categorize multiple releases.
42
     *
43
     * @param array $releases Array of ['group_id' => x, 'name' => y, 'poster' => z]
44
     * @return array Array of categorization results
45
     */
46
    public function batchCategorize(array $releases): array
47
    {
48
        $results = [];
49
50
        foreach ($releases as $release) {
51
            $groupId = $release['group_id'] ?? $release['groupId'] ?? 0;
52
            $name = $release['name'] ?? $release['releaseName'] ?? '';
53
            $poster = $release['poster'] ?? '';
54
55
            $results[] = [
56
                'release' => $release,
57
                'result' => $this->determineCategory($groupId, $name, $poster),
58
            ];
59
        }
60
61
        return $results;
62
    }
63
64
    /**
65
     * Get the underlying pipeline.
66
     */
67
    public function getPipeline(): CategorizationPipeline
68
    {
69
        return $this->pipeline;
70
    }
71
72
    /**
73
     * Add a custom categorizer to the pipeline.
74
     */
75
    public function addCategorizer(Contracts\CategorizerInterface $categorizer): self
76
    {
77
        $this->pipeline->addCategorizer($categorizer);
78
        return $this;
79
    }
80
81
    /**
82
     * Get statistics about categorizer usage.
83
     */
84
    public function getCategorizerStats(): array
85
    {
86
        $categorizers = $this->pipeline->getCategorizers();
87
88
        return $categorizers->map(function ($categorizer) {
89
            return [
90
                'name' => $categorizer->getName(),
91
                'priority' => $categorizer->getPriority(),
92
                'class' => get_class($categorizer),
93
            ];
94
        })->toArray();
95
    }
96
}
97
98