Issues (25)

src/Ganesha/GuzzleMiddleware.php (2 issues)

1
<?php
2
namespace Ackintosh\Ganesha;
3
4
use Ackintosh\Ganesha;
5
use Ackintosh\Ganesha\Exception\RejectedException;
6
use Ackintosh\Ganesha\GuzzleMiddleware\ServiceNameExtractor;
7
use Ackintosh\Ganesha\GuzzleMiddleware\ServiceNameExtractorInterface;
8
use Psr\Http\Message\RequestInterface;
9
10
class GuzzleMiddleware
11
{
12
    /**
13
     * @var \Ackintosh\Ganesha
14
     */
15
    private $ganesha;
16
17
    /*
18
     * @var ResourceNameExtractorInterface
0 ignored issues
show
The type Ackintosh\Ganesha\ResourceNameExtractorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     */
20
    private $serviceNameExtractor;
21
22
    public function __construct(
23
        Ganesha $ganesha,
24
        ServiceNameExtractorInterface $serviceNameExtractor = null
25
    ) {
26
        $this->ganesha = $ganesha;
27
        $this->serviceNameExtractor = $serviceNameExtractor ?: new ServiceNameExtractor();
0 ignored issues
show
Documentation Bug introduced by
It seems like $serviceNameExtractor ?:...\ServiceNameExtractor() of type Ackintosh\Ganesha\Guzzle...re\ServiceNameExtractor or Ackintosh\Ganesha\Guzzle...eNameExtractorInterface is incompatible with the declared type Ackintosh\Ganesha\ResourceNameExtractorInterface of property $serviceNameExtractor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    /**
31
     * @param callable $handler
32
     * @return \Closure
33
     */
34
    public function __invoke(callable $handler): \Closure
35
    {
36
        return function (RequestInterface $request, array $options) use ($handler) {
37
            $serviceName = $this->serviceNameExtractor->extract($request, $options);
38
39
            if (!$this->ganesha->isAvailable($serviceName)) {
40
                return \GuzzleHttp\Promise\rejection_for(
41
                    new RejectedException(
42
                        sprintf('"%s" is not available', $serviceName)
43
                    )
44
                );
45
            }
46
47
            $promise = $handler($request, $options);
48
49
            return $promise->then(
50
                function ($value) use ($serviceName) {
51
                    $this->ganesha->success($serviceName);
52
                    return \GuzzleHttp\Promise\promise_for($value);
53
                },
54
                function ($reason) use ($serviceName) {
55
                    $this->ganesha->failure($serviceName);
56
                    return \GuzzleHttp\Promise\rejection_for($reason);
57
                }
58
            );
59
        };
60
    }
61
}
62