1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Adlogix package. |
4
|
|
|
* |
5
|
|
|
* (c) Allan Segebarth <[email protected]> |
6
|
|
|
* (c) Jean-Jacques Courtens <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Adlogix\ConfluenceClient\Exception; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use GuzzleHttp\Exception\RequestException; |
16
|
|
|
use JMS\Serializer\SerializerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ExceptionWrapper |
20
|
|
|
* @package Adlogix\ConfluenceClient\Exception |
21
|
|
|
* @author Cedric Michaux <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ExceptionWrapper |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var SerializerInterface |
27
|
|
|
*/ |
28
|
|
|
private $serializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ExceptionWrapper constructor. |
32
|
|
|
* |
33
|
|
|
* @param SerializerInterface $serializer |
34
|
|
|
*/ |
35
|
|
|
private function __construct(SerializerInterface $serializer) |
36
|
|
|
{ |
37
|
|
|
$this->serializer = $serializer; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function wrap(RequestException $exception, SerializerInterface $serializer) |
41
|
|
|
{ |
42
|
|
|
$wrapper = new static($serializer); |
43
|
|
|
return $wrapper->parseException($exception); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param RequestException $exception |
48
|
|
|
* |
49
|
|
|
* @return ApiException |
50
|
|
|
*/ |
51
|
|
|
private function parseException(RequestException $exception) |
52
|
|
|
{ |
53
|
|
|
if ($exception->getCode() == 401) { |
54
|
|
|
return new ApiException($this->create401ErrorMessage($exception), $exception->getCode(), $exception); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new ApiException($exception->getMessage(), $exception->getCode(), $exception); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param RequestException $exception |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
private function create401ErrorMessage(RequestException $exception) |
66
|
|
|
{ |
67
|
|
|
$request = $exception->getRequest(); |
68
|
|
|
$response = $exception->getResponse(); |
69
|
|
|
|
70
|
|
|
$uri = $request->getUri(); |
71
|
|
|
$queryParams = \GuzzleHttp\Psr7\parse_query($uri->getQuery()); |
72
|
|
|
|
73
|
|
|
if ( |
74
|
|
|
!$request->hasHeader('Authorization') |
75
|
|
|
&& !$request->hasHeader('Authentication') |
76
|
|
|
&& !array_key_exists('jwt', $queryParams) |
77
|
|
|
&& empty($queryParams['jwt']) |
78
|
|
|
) { |
79
|
|
|
return 'Authentication Required'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
switch ($response->getHeader('X-Seraph-LoginReason')) { |
83
|
|
|
case 'AUTHENTICATED_FAILED': |
84
|
|
|
$msg = 'Could not be authenticated'; |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case 'AUTHENTICATION_DENIED': |
88
|
|
|
$msg = 'Not allowed to login'; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'AUTHORIZATION_FAILED': |
92
|
|
|
case 'AUTHORISATION_FAILED': |
93
|
|
|
$msg = 'Could not be authorised'; |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
|
case 'OUT': |
97
|
|
|
$msg = 'Logged out'; |
98
|
|
|
break; |
99
|
|
|
default: |
100
|
|
|
$msg = 'Invalid Credentials'; |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $msg; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|