NovaLoginMethod::login()   A
last analyzed

Complexity

Conditions 4
Paths 13

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.686

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 23
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 41
ccs 13
cts 20
cp 0.65
crap 4.686
rs 9.552
1
<?php
2
3
namespace OrcaServices\NovaApi\Method;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use OrcaServices\NovaApi\Exception\NovaApiUnauthorizedException;
8
use OrcaServices\NovaApi\Parser\NovaApiErrorParser;
9
use UnexpectedValueException;
10
11
/**
12
 * SOAP method.
13
 */
14
final class NovaLoginMethod implements NovaMethod
15
{
16
    /**
17
     * @var NovaApiErrorParser
18
     */
19
    private $novaApiErrorParser;
20
21
    /**
22
     * @var Client
23
     */
24
    private $client;
25
26
    /**
27
     * NovaLoginMethod constructor.
28
     *
29
     * @param Client $client The client
30
     * @param NovaApiErrorParser $novaApiErrorParser The error handler
31
     */
32 13
    public function __construct(Client $client, NovaApiErrorParser $novaApiErrorParser)
33
    {
34 13
        $this->client = $client;
35 13
        $this->novaApiErrorParser = $novaApiErrorParser;
36 13
    }
37
38
    /**
39
     * Request an authentication token.
40
     *
41
     * The authentication token must be transferred with every webservice request in
42
     * the HTTP header (Cookie: SAML-Ticket=<authentication token>).
43
     *
44
     * https://confluence-ext.sbb.ch/display/NOVAUG/Authentication+and+Authorization+via+SAML
45
     *
46
     * @param string $clientId The WSG credential username
47
     * @param string $clientSecret The WSG credential password
48
     *
49
     * @throws NovaApiUnauthorizedException
50
     * @throws UnexpectedValueException
51
     *
52
     * @return string the authentication The authentication token returned by the login call
53
     */
54 13
    public function login(string $clientId, string $clientSecret): string
55
    {
56
        $options = [
57 13
            'body' => http_build_query(
58
                [
59 13
                    'grant_type' => 'client_credentials',
60 13
                    'client_id' => $clientId,
61 13
                    'client_secret' => $clientSecret,
62
                ]
63
            ),
64
            'headers' => [
65
                'Content-Type' => 'application/x-www-form-urlencoded',
66
            ],
67
        ];
68
69
        try {
70 13
            $response = $this->client->request(
71 13
                'POST',
72 13
                '/auth/realms/SBB_Public/protocol/openid-connect/token',
73 13
                $options
74
            );
75
76 13
            $body = (string)$response->getBody();
77
78 13
            if (strpos($body, '{') === false) {
79
                throw new UnexpectedValueException(
80
                    'Oauth2 authentication failed. Invalid json response. Access token not found.'
81
                );
82
            }
83
84 13
            $result = json_decode($body, true);
85
86 13
            return (string)$result['access_token'];
87
        } catch (Exception $ex) {
88
            $message = $this->novaApiErrorParser->getExceptionMessage($ex);
89
90
            if ($ex->getCode() === 401) {
91
                throw new NovaApiUnauthorizedException($message, $ex->getCode(), $ex);
92
            }
93
94
            throw new UnexpectedValueException($message, $ex->getCode(), $ex);
95
        }
96
    }
97
}
98