Completed
Push — master ( 6e52f0...d9a404 )
by Alexandre
02:29
created

ErrorResponse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
ccs 0
cts 26
cp 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A containsNotAsciiChar() 0 3 1
C __construct() 0 29 7
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 31/12/2017
6
 * Time: 17:05
7
 */
8
9
namespace OAuth2OLD\Endpoint\Server\Messages\Authorization;
10
11
12
use GuzzleHttp\Psr7\Response;
13
use GuzzleHttp\Psr7\Uri;
14
use Psr\Http\Message\UriInterface;
15
16
class ErrorResponse extends Response
17
{
18
    const errors = [
19
        'invalid_request',
20
        'unauthorized_client',
21
        'access_denied',
22
        'unsupported_response_type',
23
        'invalid_scope',
24
        'server_error',
25
        'temporarily_unavailable',
26
    ];
27
28
    /**
29
     * ErrorResponse constructor.
30
     * @param UriInterface $redirectUri
31
     * @param string $error
32
     * @param null|string $errorDescription
33
     * @param null|string $errorUri
34
     * @throws \Exception
35
     */
36
    public function __construct(UriInterface $redirectUri, string $error, ?string $errorDescription = null, ?string $errorUri = null)
37
    {
38
        if ($this->containsNotAsciiChar($error)) {
39
            throw new \Exception('Malformed error type. Expect ascii string. Got : ' . $error);
40
        }
41
42
        if (!in_array($error, self::errors)) {
43
            throw new \Exception('Unexpected error type. Expect one of : ' . implode(', ', self::errors));
44
        }
45
46
        $redirectUri = Uri::withQueryValue($redirectUri, 'error', $error);
47
48
        if ($errorDescription) {
49
            if ($this->containsNotAsciiChar($errorDescription)) {
50
                throw new \Exception('Malformed error description. Expect ascii string. Got : ' . $errorDescription);
51
            }
52
53
            $redirectUri = Uri::withQueryValue($redirectUri, 'error_description', $errorDescription);
54
        }
55
56
        if ($errorUri) {
57
            if ($this->containsNotAsciiChar($errorUri)) {
58
                throw new \Exception('Malformed error uri. Expect ascii string. Got : ' . $errorUri);
59
            }
60
61
            $redirectUri = Uri::withQueryValue($redirectUri, 'error_uri', $errorUri);
62
        }
63
64
        parent::__construct(302, ['Location' => $redirectUri->__toString()]);
65
    }
66
67
    private function containsNotAsciiChar(string $string)
68
    {
69
        return preg_match('/[^\x20-\x7e]/', $string);
70
    }
71
}