• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ducks-project / encoding-repair / 21228444070

21 Jan 2026 10:45PM UTC coverage: 75.976% (+4.7%) from 71.26%
21228444070

push

github

donaldinou
feat : add object oriented transcoders

97 of 127 new or added lines in 6 files covered. (76.38%)

253 of 333 relevant lines covered (75.98%)

7.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/Transcoder/TranscoderChain.php
1
<?php
2

3
/**
4
 * Part of EncodingRepair package.
5
 *
6
 * (c) Adrien Loyant <donald_duck@team-df.org>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Ducks\Component\EncodingRepair\Transcoder;
15

16
use SplPriorityQueue;
17

18
/**
19
 * Chain of Responsibility for transcoders with priority management.
20
 *
21
 * @final
22
 */
23
final class TranscoderChain
24
{
25
    /**
26
     * @var null|SplPriorityQueue<int, TranscoderInterface>
27
     */
28
    private ?SplPriorityQueue $queue;
29

30
    /**
31
     * @var list<array{transcoder: TranscoderInterface, priority: int}>
32
     */
33
    private $registered = [];
34

35
    public function __construct()
19✔
36
    {
37
        $this->queue = null;
19✔
38
    }
39

40
    /**
41
     * Register a transcoder with optional priority override.
42
     *
43
     * @param TranscoderInterface $transcoder Transcoder instance
44
     * @param int|null $priority Priority override (null = use transcoder's default)
45
     *
46
     * @return void
47
     */
48
    public function register(TranscoderInterface $transcoder, ?int $priority = null): void
19✔
49
    {
50
        $finalPriority = $priority ?? $transcoder->getPriority();
19✔
51

52
        $this->registered[] = [
19✔
53
            'transcoder' => $transcoder,
19✔
54
            'priority' => $finalPriority,
19✔
55
        ];
19✔
56

57
        $this->getSplPriorityQueue()->insert($transcoder, $finalPriority);
19✔
58
    }
59

60
    /**
61
     * Execute transcoding using chain of responsibility.
62
     *
63
     * @param string $data Data to transcode
64
     * @param string $to Target encoding
65
     * @param string $from Source encoding
66
     * @param array<string, mixed> $options Conversion options
67
     *
68
     * @return string|null Transcoded string or null if all failed
69
     */
70
    public function transcode(string $data, string $to, string $from, array $options): ?string
18✔
71
    {
72
        $this->rebuildQueue();
18✔
73

74
        foreach ($this->getSplPriorityQueue() as $transcoder) {
18✔
75
            if (!$transcoder->isAvailable()) {
18✔
NEW
76
                continue;
×
77
            }
78

79
            $result = $transcoder->transcode($data, $to, $from, $options);
18✔
80

81
            if (null !== $result) {
18✔
82
                return $result;
18✔
83
            }
84
        }
85

NEW
86
        return null;
×
87
    }
88

89
    /**
90
     * Rebuild queue from registered transcoders.
91
     *
92
     * @return void
93
     */
94
    private function rebuildQueue(): void
18✔
95
    {
96
        $this->queue = new SplPriorityQueue();
18✔
97

98
        foreach ($this->registered as $item) {
18✔
99
            $this->queue->insert($item['transcoder'], $item['priority']);
18✔
100
        }
101
    }
102

103
    /**
104
     * Return the queue.
105
     *
106
     * @return SplPriorityQueue<int, TranscoderInterface>
107
     */
108
    private function getSplPriorityQueue(): SplPriorityQueue
19✔
109
    {
110
        if (!$this->queue instanceof SplPriorityQueue) {
19✔
111
            $this->queue = new SplPriorityQueue();
19✔
112
        }
113

114
        return $this->queue;
19✔
115
    }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc