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 ( 1b18fc...15e59d )
by Cees-Jan
04:01
created

AsyncClient::repository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 ApiClients\Foundation\Resource\ResourceInterface;
10
use React\EventLoop\LoopInterface;
11
use React\Promise\CancellablePromiseInterface;
12
use React\Promise\PromiseInterface;
13
use Rx\ObservableInterface;
14
use Rx\React\Promise;
15
use Rx\Scheduler;
16
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
17
18
final class AsyncClient implements AsyncClientInterface
19
{
20
    /**
21
     * @var ClientInterface
22
     */
23
    private $client;
24
25
    /**
26
     * @param ClientInterface $client
27
     */
28 9
    private function __construct(ClientInterface $client)
29
    {
30 9
        $this->client = $client;
31 9
    }
32
33
    /**
34
     * Create a new AsyncClient based on the loop and other options pass.
35
     *
36
     * @param  LoopInterface $loop
37
     * @param  string        $token   Instructions to fetch the token: https://blog.travis-ci.com/2013-01-28-token-token-token/
38
     * @param  array         $options
39
     * @return AsyncClient
40
     */
41 1
    public static function create(
42
        LoopInterface $loop,
43
        string $token = '',
44
        array $options = []
45
    ): self {
46
        try {
47
            Scheduler::setAsyncFactory(function () use ($loop) {
48
                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...
49 1
            });
50
        } catch (\Throwable $t) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
51
        }
52
53 1
        $options = ApiSettings::getOptions($token, 'Async', $options);
54 1
        $client = Factory::create($loop, $options);
55
56 1
        return new self($client);
57
    }
58
59
    public function hydrate(string $resource): CancellablePromiseInterface
60
    {
61
        return $this->client->hydrate($resource);
62
    }
63
64
    public function extract(ResourceInterface $resource): CancellablePromiseInterface
65
    {
66
        return $this->client->extract($resource);
67
    }
68
69
    /**
70
     * Create an AsyncClient from a ApiClients\Foundation\ClientInterface.
71
     * Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix.
72
     *
73
     * @param  ClientInterface $client
74
     * @return AsyncClient
75
     */
76 8
    public static function createFromClient(ClientInterface $client): self
77
    {
78 8
        return new self($client);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public function repository(string $repository): CancellablePromiseInterface
85
    {
86 1
        return $this->client->handle(new Command\RepositoryCommand($repository));
87
    }
88
89
    /**
90
     * Warning this a intensive operation as it has to make a call for each hook
91
     * to turn it into a Repository!!!
92
     *
93
     * Take a look at examples/repositories-async.php on how to limit the amount of
94
     * concurrent requests.
95
     *
96
     * {@inheritdoc}
97
     */
98
    public function repositories(callable $filter = null): ObservableInterface
99
    {
100
        if ($filter === null) {
101
            $filter = function () {
102
                return true;
103
            };
104
        }
105
106
        return $this->hooks()->filter(function ($hook) {
107
            return $hook->active();
108
        })->filter($filter)->flatMap(function (HookInterface $hook) {
109
            return Promise::toObservable($this->client->handle(
110
                new Command\RepositoryIdCommand($hook->id())
111
            ));
112
        });
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function user(): PromiseInterface
119
    {
120 1
        return $this->client->handle(new Command\UserCommand());
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 1
    public function sshKey(int $id): PromiseInterface
127
    {
128 1
        return $this->client->handle(new Command\SSHKeyCommand($id));
129
    }
130
131
    /**
132
     * @return ObservableInterface
133
     */
134 1
    public function hooks(): ObservableInterface
135
    {
136 1
        return unwrapObservableFromPromise($this->client->handle(
137 1
            new Command\HooksCommand()
138
        ));
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function accounts(): ObservableInterface
145
    {
146 1
        return unwrapObservableFromPromise($this->client->handle(
147 1
            new Command\AccountsCommand()
148
        ));
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 1
    public function broadcasts(): ObservableInterface
155
    {
156 1
        return unwrapObservableFromPromise($this->client->handle(
157 1
            new Command\BroadcastsCommand()
158
        ));
159
    }
160
}
161