|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services\Categorization\Pipes; |
|
4
|
|
|
|
|
5
|
|
|
use App\Services\Categorization\CategorizationResult; |
|
6
|
|
|
use App\Services\Categorization\ReleaseContext; |
|
7
|
|
|
use Closure; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base class for categorization pipe handlers. |
|
11
|
|
|
* |
|
12
|
|
|
* Each pipe is responsible for categorizing a specific type of release. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractCategorizationPipe |
|
15
|
|
|
{ |
|
16
|
|
|
protected int $priority = 50; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handle the categorization request. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function handle(CategorizationPassable $passable, Closure $next): CategorizationPassable |
|
22
|
|
|
{ |
|
23
|
|
|
// If we already have a high-confidence match, skip processing |
|
24
|
|
|
if ($passable->shouldStopProcessing()) { |
|
25
|
|
|
return $next($passable); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// Skip if this categorizer shouldn't process this release |
|
29
|
|
|
if ($this->shouldSkip($passable->context)) { |
|
30
|
|
|
return $next($passable); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// Attempt categorization |
|
34
|
|
|
$result = $this->categorize($passable->context); |
|
35
|
|
|
|
|
36
|
|
|
// Update the best result |
|
37
|
|
|
$passable->updateBestResult($result, $this->getName()); |
|
38
|
|
|
|
|
39
|
|
|
return $next($passable); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the priority of this categorizer (lower = higher priority). |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getPriority(): int |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->priority; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the name of this categorizer for debugging/logging. |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract public function getName(): string; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Attempt to categorize the given release. |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract protected function categorize(ReleaseContext $context): CategorizationResult; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Check if this categorizer should be skipped for the given context. |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function shouldSkip(ReleaseContext $context): bool |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create a successful categorization result. |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function matched(int $categoryId, float $confidence, string $matchedBy, array $debug = []): CategorizationResult |
|
72
|
|
|
{ |
|
73
|
|
|
return new CategorizationResult($categoryId, $confidence, $matchedBy, $debug); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create a no-match result. |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function noMatch(): CategorizationResult |
|
80
|
|
|
{ |
|
81
|
|
|
return CategorizationResult::noMatch(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.