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 ( 63aea7...119861 )
by Cees-Jan
04:39
created

AsyncClient   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 69.44%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 14
dl 0
loc 132
ccs 25
cts 36
cp 0.6944
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromClient() 0 4 1
A __construct() 0 4 1
A repository() 0 4 1
A user() 0 4 1
A sshKey() 0 4 1
A hooks() 0 6 1
A accounts() 0 6 1
A broadcasts() 0 6 1
A create() 0 16 2
A repositories() 0 16 2
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Travis;
4
5
use ApiClients\Client\Travis\CommandBus\Command;
6
use ApiClients\Client\Travis\Resource\HookInterface;
7
use ApiClients\Foundation\ClientInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiClients\Client\Travis\ClientInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use ApiClients\Foundation\Factory;
9
use React\EventLoop\LoopInterface;
10
use React\Promise\CancellablePromiseInterface;
11
use React\Promise\PromiseInterface;
12
use Rx\ObservableInterface;
13
use Rx\React\Promise;
14
use Rx\Scheduler;
15
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
16
17
final class AsyncClient implements AsyncClientInterface
18
{
19
    /**
20
     * @var ClientInterface
21
     */
22
    private $client;
23
24
    /**
25
     * Create a new AsyncClient based on the loop and other options pass
26
     *
27
     * @param LoopInterface $loop
28
     * @param string $token Instructions to fetch the token: https://blog.travis-ci.com/2013-01-28-token-token-token/
29
     * @param array $options
30
     * @return AsyncClient
31
     */
32 1
    public static function create(
33
        LoopInterface $loop,
34
        string $token = '',
35
        array $options = []
36
    ): self {
37
        try {
38
            Scheduler::setAsyncFactory(function () use ($loop) {
39
                return new Scheduler\EventLoopScheduler($loop);
0 ignored issues
show
Documentation introduced by
$loop is of type object<React\EventLoop\LoopInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40 1
            });
41
        } catch (\Throwable $t) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
42
        }
43
44 1
        $options = ApiSettings::getOptions($token, 'Async', $options);
45 1
        $client = Factory::create($loop, $options);
46 1
        return new self($client);
47
    }
48
49
    /**
50
     * Create an AsyncClient from a ApiClients\Foundation\ClientInterface.
51
     * Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix.
52
     *
53
     * @param ClientInterface $client
54
     * @return AsyncClient
55
     */
56 8
    public static function createFromClient(ClientInterface $client): self
57
    {
58 8
        return new self($client);
59
    }
60
61
    /**
62
     * @param ClientInterface $client
63
     */
64 9
    private function __construct(ClientInterface $client)
65
    {
66 9
        $this->client = $client;
67 9
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function repository(string $repository): CancellablePromiseInterface
73
    {
74 1
        return $this->client->handle(new Command\RepositoryCommand($repository));
75
    }
76
77
    /**
78
     * Warning this a intensive operation as it has to make a call for each hook
79
     * to turn it into a Repository!!!
80
     *
81
     * Take a look at examples/repositories-async.php on how to limit the amount of
82
     * concurrent requests.
83
     *
84
     * {@inheritdoc}
85
     */
86
    public function repositories(callable $filter = null): ObservableInterface
87
    {
88
        if ($filter === null) {
89
            $filter = function () {
90
                return true;
91
            };
92
        }
93
94
        return $this->hooks()->filter(function ($hook) {
95
            return $hook->active();
96
        })->filter($filter)->flatMap(function (HookInterface $hook) {
97
            return Promise::toObservable($this->client->handle(
98
                new Command\RepositoryIdCommand($hook->id())
99
            ));
100
        });
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function user(): PromiseInterface
107
    {
108 1
        return $this->client->handle(new Command\UserCommand());
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function sshKey(int $id): PromiseInterface
115
    {
116 1
        return $this->client->handle(new Command\SSHKeyCommand($id));
117
    }
118
119
    /**
120
     * @return ObservableInterface
121
     */
122 1
    public function hooks(): ObservableInterface
123
    {
124 1
        return unwrapObservableFromPromise($this->client->handle(
125 1
            new Command\HooksCommand()
126
        ));
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    public function accounts(): ObservableInterface
133
    {
134 1
        return unwrapObservableFromPromise($this->client->handle(
135 1
            new Command\AccountsCommand()
136
        ));
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function broadcasts(): ObservableInterface
143
    {
144 1
        return unwrapObservableFromPromise($this->client->handle(
145 1
            new Command\BroadcastsCommand()
146
        ));
147
    }
148
}
149