GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b7e0a9...e2927f )
by Cees-Jan
08:19
created

AsyncClient::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 5
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Client\RabbitMQ\Management;
5
6
use ApiClients\Foundation\ClientInterface as FoundationClientInterface;
7
use ApiClients\Foundation\Factory;
8
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
9
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
10
use Psr\Http\Message\ResponseInterface;
11
use React\EventLoop\LoopInterface;
12
use React\Promise\PromiseInterface;
13
use Rx\Observable;
14
use Rx\ObservableInterface;
15
use Rx\React\Promise;
16
use function React\Promise\resolve;
17
18
final class AsyncClient implements AsyncClientInterface
19
{
20
    /**
21
     * @var FoundationClientInterface
22
     */
23
    private $client;
24
25
    /**
26
     * Create a new AsyncClient based on the loop and other options pass
27
     *
28
     * @param LoopInterface $loop
29
     * @param string $baseUrl
30
     * @param string $username
31
     * @param string $password
32
     * @param array $options
33
     * @return AsyncClient
34
     */
35
    public static function create(
36
        LoopInterface $loop,
37
        string $baseUrl,
38
        string $username,
39
        string $password,
40
        array $options = []
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    ): self {
42
        $options = ApiSettings::getOptions($baseUrl, $username, $password, 'Async');
43
        $client = Factory::create($loop, $options);
44
        return new self($client);
45
    }
46
47
    /**
48
     * Create an AsyncClient from a ApiClients\Foundation\ClientInterface.
49
     * Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix.
50
     *
51
     * @param FoundationClientInterface $client
52
     * @return AsyncClient
53
     */
54
    public static function createFromClient(FoundationClientInterface $client): self
55
    {
56
        return new self($client);
57
    }
58
59
    /**
60
     * @param FoundationClientInterface $client
61
     */
62
    private function __construct(FoundationClientInterface $client)
63
    {
64
        $this->client = $client;
65
    }
66
67
    /**
68
     * @return PromiseInterface
69
     */
70
    public function overview(): PromiseInterface
71
    {
72
        return $this->client->handle(
73
            new SimpleRequestCommand('overview')
74
        )->then(function (ResponseInterface $response) {
75
            return resolve($this->client->handle(
76
                new HydrateCommand('Overview', $response->getBody()->getJson())
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method getJson() does only exist in the following implementations of said interface: ApiClients\Foundation\Transport\JsonStream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
77
            ));
78
        });
79
    }
80
81
    /**
82
     * @return ObservableInterface
83
     */
84 View Code Duplication
    public function queues(): ObservableInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        return Promise::toObservable($this->client->handle(
87
            new SimpleRequestCommand('queues')
88
        ))->flatMap(function (ResponseInterface $response) {
89
            return Observable::fromArray($response->getBody()->getJson());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method getJson() does only exist in the following implementations of said interface: ApiClients\Foundation\Transport\JsonStream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
        })->flatMap(function ($queue) {
91
            return Promise::toObservable($this->client->handle(
92
                new HydrateCommand('Queue', $queue)
93
            ));
94
        });
95
    }
96
97
    /**
98
     * @return ObservableInterface
99
     */
100 View Code Duplication
    public function connections(): ObservableInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        return Promise::toObservable($this->client->handle(
103
            new SimpleRequestCommand('connections')
104
        ))->flatMap(function (ResponseInterface $response) {
105
            return Observable::fromArray($response->getBody()->getJson());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method getJson() does only exist in the following implementations of said interface: ApiClients\Foundation\Transport\JsonStream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
106
        })->flatMap(function ($connection) {
107
            return Promise::toObservable($this->client->handle(
108
                new HydrateCommand('Connection', $connection)
109
            ));
110
        });
111
    }
112
}
113