Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
12 / 13 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SynchronousIcapClient | |
92.31% |
12 / 13 |
|
83.33% |
5 / 6 |
6.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| request | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| options | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| scanFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| scanFileWithPreview | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ndrstmr\Icap; |
| 6 | |
| 7 | use Amp\Future; |
| 8 | use Ndrstmr\Icap\DTO\IcapRequest; |
| 9 | use Ndrstmr\Icap\DTO\ScanResult; |
| 10 | use Ndrstmr\Icap\Config; |
| 11 | use Ndrstmr\Icap\Transport\SynchronousStreamTransport; |
| 12 | use Ndrstmr\Icap\RequestFormatter; |
| 13 | use Ndrstmr\Icap\ResponseParser; |
| 14 | use Ndrstmr\Icap\DefaultPreviewStrategy; |
| 15 | |
| 16 | /** |
| 17 | * Simple synchronous wrapper around {@link IcapClient}. |
| 18 | */ |
| 19 | final class SynchronousIcapClient |
| 20 | { |
| 21 | private IcapClient $asyncClient; |
| 22 | |
| 23 | /** |
| 24 | * @param IcapClient $asyncClient Underlying asynchronous client |
| 25 | */ |
| 26 | public function __construct(IcapClient $asyncClient) |
| 27 | { |
| 28 | $this->asyncClient = $asyncClient; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Create a client with default configuration. |
| 33 | */ |
| 34 | public static function create(): self |
| 35 | { |
| 36 | $asyncClient = new IcapClient( |
| 37 | new Config('127.0.0.1'), |
| 38 | new SynchronousStreamTransport(), |
| 39 | new RequestFormatter(), |
| 40 | new ResponseParser(), |
| 41 | new DefaultPreviewStrategy(), |
| 42 | ); |
| 43 | |
| 44 | return new self($asyncClient); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param IcapRequest $request |
| 49 | */ |
| 50 | public function request(IcapRequest $request): ScanResult |
| 51 | { |
| 52 | return $this->asyncClient->request($request)->await(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param string $service |
| 57 | */ |
| 58 | public function options(string $service): ScanResult |
| 59 | { |
| 60 | return $this->asyncClient->options($service)->await(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @throws \RuntimeException |
| 65 | */ |
| 66 | public function scanFile(string $service, string $filePath): ScanResult |
| 67 | { |
| 68 | return $this->asyncClient->scanFile($service, $filePath)->await(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @throws \RuntimeException |
| 73 | */ |
| 74 | public function scanFileWithPreview(string $service, string $filePath, int $previewSize = 1024): ScanResult |
| 75 | { |
| 76 | return $this->asyncClient->scanFileWithPreview($service, $filePath, $previewSize)->await(); |
| 77 | } |
| 78 | } |