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
Pull Request — master (#13)
by Cees-Jan
07:48
created

AsyncClient::hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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