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.

ModelHydrator::hydrate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 20
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pnz\MattermostClient\Hydrator;
6
7
use Pnz\JsonException\Json;
8
use Pnz\MattermostClient\Exception\HydrationException;
9
use Pnz\MattermostClient\Model\CreatableFromArray;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Hydrate an HTTP response to domain object.
14
 */
15
class ModelHydrator implements Hydrator
16
{
17
    public function hydrate(ResponseInterface $response, string $class)
18
    {
19
        $body = (string) $response->getBody();
20
21
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
22
            throw new HydrationException('The ModelHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
23
        }
24
25
        try {
26
            $data = Json::decode($body, true);
27
        } catch (\JsonException $exception) {
28
            throw new HydrationException(sprintf('Error when trying to decode the JSON response: %s', $exception->getMessage()), 0, $exception);
29
        }
30
31
        if (is_subclass_of($class, CreatableFromArray::class)) {
32
            $object = $class::createFromArray($data);
33
        } else {
34
            $object = new $class($data);
35
        }
36
37
        return $object;
38
    }
39
}
40