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   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 11
cp 0
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 21 4
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