GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 164506...170504 )
by Freek
03:32 queued 02:30
created

DefaultSerializer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 5
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 4 1
A unserialize() 0 14 2
A getResponseData() 0 17 2
A containsValidResponseProperties() 0 12 3
A buildResponse() 0 13 2
1
<?php
2
3
namespace Spatie\ResponseCache\Serializer;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Illuminate\Http\Response as IlluminateResponse;
7
use Spatie\ResponseCache\Exceptions\CouldNotUnserialize;
8
use Symfony\Component\HttpFoundation\BinaryFileResponse;
9
10
class DefaultSerializer implements Serializable
11
{
12
    public const RESPONSE_TYPE_NORMAL = 'response_type_normal';
13
    public const RESPONSE_TYPE_FILE = 'response_type_file';
14
15
    public function serialize(Response $response): string
16
    {
17
        return serialize($this->getResponseData($response));
18
    }
19
20
    public function unserialize(string $serializedResponse): Response
21
    {
22
        $responseProperties = unserialize($serializedResponse);
23
24
        if (! $this->containsValidResponseProperties($responseProperties)) {
25
            throw CouldNotUnserialize::serializedResponse($serializedResponse);
26
        }
27
28
        $response = $this->buildResponse($responseProperties);
29
30
        $response->headers = $responseProperties['headers'];
31
32
        return $response;
33
    }
34
35
    protected function getResponseData(Response $response): array
36
    {
37
        $statusCode = $response->getStatusCode();
38
        $headers = $response->headers;
39
40
        if ($response instanceof BinaryFileResponse) {
41
            $content = $response->getFile()->getPathname();
42
            $type = self::RESPONSE_TYPE_FILE;
43
44
            return compact('statusCode', 'headers', 'content', 'type');
45
        }
46
47
        $content = $response->getContent();
48
        $type = self::RESPONSE_TYPE_NORMAL;
49
50
        return compact('statusCode', 'headers', 'content', 'type');
51
    }
52
53
    protected function containsValidResponseProperties($properties): bool
54
    {
55
        if (! is_array($properties)) {
56
            return false;
57
        }
58
59
        if (! isset($properties['content'], $properties['statusCode'])) {
60
            return false;
61
        }
62
63
        return true;
64
    }
65
66
    protected function buildResponse(array $responseProperties): Response
67
    {
68
        $type = $responseProperties['type'] ?? self::RESPONSE_TYPE_NORMAL;
69
70
        if ($type === self::RESPONSE_TYPE_FILE) {
71
            return new BinaryFileResponse(
72
                $responseProperties['content'],
73
                $responseProperties['statusCode']
74
            );
75
        }
76
77
        return new IlluminateResponse($responseProperties['content'], $responseProperties['statusCode']);
78
    }
79
}
80