Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
SynchronousIcapClient
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 request
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 options
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 scanFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 scanFileWithPreview
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SPDX-License-Identifier: EUPL-1.2
5 *
6 * This file is part of icap-flow.
7 *
8 * Licensed under the EUPL, Version 1.2 only (the "Licence");
9 * you may not use this work except in compliance with the Licence.
10 * You may obtain a copy of the Licence at:
11 *
12 *     https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the Licence is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 */
18
19declare(strict_types=1);
20
21namespace Ndrstmr\Icap;
22
23use Amp\Cancellation;
24use Amp\Future;
25use Ndrstmr\Icap\DTO\IcapRequest;
26use Ndrstmr\Icap\DTO\IcapResponse;
27use Ndrstmr\Icap\DTO\ScanResult;
28use Ndrstmr\Icap\Config;
29use Ndrstmr\Icap\Transport\SynchronousStreamTransport;
30use Ndrstmr\Icap\RequestFormatter;
31use Ndrstmr\Icap\ResponseParser;
32use Ndrstmr\Icap\DefaultPreviewStrategy;
33
34/**
35 * Simple synchronous wrapper around {@link IcapClient}.
36 */
37final class SynchronousIcapClient
38{
39    private IcapClientInterface $asyncClient;
40
41    /**
42     * @param IcapClientInterface $asyncClient Underlying asynchronous client
43     */
44    public function __construct(IcapClientInterface $asyncClient)
45    {
46        $this->asyncClient = $asyncClient;
47    }
48
49    /**
50     * Create a client with default configuration.
51     */
52    public static function create(): self
53    {
54        $asyncClient = new IcapClient(
55            new Config('127.0.0.1'),
56            new SynchronousStreamTransport(),
57            new RequestFormatter(),
58            new ResponseParser(),
59            new DefaultPreviewStrategy(),
60        );
61
62        return new self($asyncClient);
63    }
64
65    public function request(IcapRequest $request, ?Cancellation $cancellation = null): ScanResult
66    {
67        return $this->asyncClient->request($request, $cancellation)->await();
68    }
69
70    public function options(string $service, ?Cancellation $cancellation = null): IcapResponse
71    {
72        return $this->asyncClient->options($service, $cancellation)->await();
73    }
74
75    /**
76     * @param array<string, string|string[]> $extraHeaders
77     * @throws \RuntimeException
78     */
79    public function scanFile(
80        string $service,
81        string $filePath,
82        array $extraHeaders = [],
83        ?Cancellation $cancellation = null,
84    ): ScanResult {
85        return $this->asyncClient->scanFile($service, $filePath, $extraHeaders, $cancellation)->await();
86    }
87
88    /**
89     * @param array<string, string|string[]> $extraHeaders
90     * @throws \RuntimeException
91     */
92    public function scanFileWithPreview(
93        string $service,
94        string $filePath,
95        ?int $previewSize = null,
96        array $extraHeaders = [],
97        ?Cancellation $cancellation = null,
98    ): ScanResult {
99        return $this->asyncClient->scanFileWithPreview($service, $filePath, $previewSize, $extraHeaders, $cancellation)->await();
100    }
101}