Passed
Pull Request — develop (#20)
by Stephen
05:22
created

LogoutDiscourseUser::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 26
ccs 0
cts 15
cp 0
crap 12
rs 9.7333
1
<?php
2
3
namespace Spinen\Discourse\Listeners;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Auth\Events\Logout;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * Class LogoutDiscourseUser
12
 *
13
 * When logging out a Laravel user, send a Logout request to Discourse for that user also.
14
 *
15
 * @package Spinen\Discourse\Listeners
16
 */
17
class LogoutDiscourseUser implements ShouldQueue
18
{
19
    /**
20
     * @var Client
21
     */
22
    public $client;
23
24
    /**
25
     * Create the event listener.
26
     *
27
     * @param Client $client
28
     *
29
     * @return void
30
     */
31 1
    public function __construct(Client $client)
32
    {
33 1
        $this->client = $client;
34
    }
35
36
    /**
37
     * Handle the event.
38
     *
39
     * @param Logout $event
40
     *
41
     * @return void
42
     */
43
    public function handle(Logout $event)
44
    {
45
        if (!$event->user) {
46
            return;
47
        }
48
49
        $configs = [
50
            'base_uri' => config('services.discourse.url'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            'base_uri' => /** @scrutinizer ignore-call */ config('services.discourse.url'),
Loading history...
51
            'headers'  => [
52
                'Api-Key'      => config('services.discourse.api.key'),
53
                'Api-Username' => config('services.discourse.api.user'),
54
            ],
55
        ];
56
57
        // Get Discourse user to match this one, and send a Logout request to Discourse and get the response
58
        $user = json_decode(
59
            $this->client->get("users/by-external/{$event->user->id}.json", $configs)
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
60
                         ->getBody()
61
        )->user;
62
63
        $response = $this->client->post("admin/users/{$user->id}/log_out");
64
65
        if ($response->getStatusCode() !== 200) {
66
            Log::notice(
67
                "When logging out user {$event->user->id} Discourse returned status code {$response->getStatusCode()}:",
68
                ['reason' => $response->getReasonPhrase()]
69
            );
70
        }
71
    }
72
}
73