Passed
Push — master ( 3166e9...bc6228 )
by Dan Michael O.
03:17
created

WebhooksController::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 2
dl 0
loc 22
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\User;
6
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Http\Response;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Scriptotek\Alma\Client;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class WebhooksController extends Controller
11
{
12
    /**
13
     * Alma webhooks router.
14
     *
15
     * @param Request $request
16
     * @param Client $almaClient
17
     * @return Response
18
     */
19
    public function handle(Request $request, Client $almaClient)
20
    {
21
        $secret = config('alma.webhook_secret');
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

21
        $secret = /** @scrutinizer ignore-call */ config('alma.webhook_secret');
Loading history...
22
        $eventType = $request->input('action');
23
        $hash = $request->header('X-Exl-Signature');
24
        $expectedHash = 'sha256=' . hash_hmac('sha256', $request->getContent(), $secret);
25
26
        if ($hash !== $expectedHash) {
27
            \Log::warning(
28
                "Ignoring Alma '{$eventType}' event due to invalid signature. " .
29
                "Expected '{$expectedHash}', got '{$hash}'."
30
            );
31
32
            return response()->json(['errorMessage' => 'Invalid Signature'], 401);
0 ignored issues
show
Bug introduced by
The function response 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

32
            return /** @scrutinizer ignore-call */ response()->json(['errorMessage' => 'Invalid Signature'], 401);
Loading history...
33
        }
34
35
        switch ($eventType) {
36
            case 'USER':
37
                return $this->handleUserUpdate($almaClient, $request->input('webhook_user'));
38
39
            default:
40
                return response('No handler for this webhook event type.', 202);
41
        }
42
    }
43
44
    /**
45
     * Handler for the Alma user webhook.
46
     *
47
     * @param Client $almaClient
48
     * @param $data
49
     * @return Response
50
     */
51
    protected function handleUserUpdate(Client $almaClient, $data)
52
    {
53
        \Log::info("Received user update notification from Alma. Type {$data->method}, caused by {$data->cause}.");
54
55
        $almaClientUser = \Scriptotek\Alma\Users\User::make($almaClient, $data->user->user_id)
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Users\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
            ->setData($data->user);
57
58
        $almaUser = new \App\Alma\User($almaClientUser);
59
60
        $localUser = User::where('alma_primary_id', '=', $almaUser->primaryId)
61
            ->orWhere('university_id', '=', $almaUser->getUniversityId())
62
            ->first();
63
64
        if (is_null($localUser)) {
65
            \Log::debug('User not in Bibrex');
66
        } else {
67
            $localUser->mergeFromAlmaResponse($almaUser);
68
            $localUser->save();
69
            \Log::debug('Updated user with changes from Alma: ' . $localUser->id);
70
        }
71
72
        // Say yo to Alma
73
        return response('Yo!', 200);
0 ignored issues
show
Bug introduced by
The function response 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

73
        return /** @scrutinizer ignore-call */ response('Yo!', 200);
Loading history...
74
    }
75
}
76