Passed
Push — master ( 6ffd57...b56fef )
by Nikolay
03:00
created

WebhookFetcher::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase;
6
7
use Psr\Http\Message\RequestInterface;
8
use TgBotApi\BotApiBase\Exception\BadRequestException;
9
use TgBotApi\BotApiBase\Type\UpdateType;
10
11
/**
12
 * Class WebhookFetcher.
13
 */
14
class WebhookFetcher implements WebhookFetcherInterface
15
{
16
    /**
17
     * @var BotApiNormalizer
18
     */
19
    private $normalizer;
20
21
    /**
22
     * WebhookFetcher constructor.
23
     *
24
     * @param NormalizerInterface $normalizer
25
     */
26 3
    public function __construct(NormalizerInterface $normalizer)
27
    {
28 3
        $this->normalizer = $normalizer;
0 ignored issues
show
Documentation Bug introduced by
$normalizer is of type TgBotApi\BotApiBase\NormalizerInterface, but the property $normalizer was declared to be of type TgBotApi\BotApiBase\BotApiNormalizer. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
29 3
    }
30
31
    /**
32
     * @param RequestInterface|string $request
33
     *
34
     * @throws BadRequestException
35
     *
36
     * @return UpdateType
37
     */
38 3
    public function fetch($request): UpdateType
39
    {
40 3
        $input = \json_decode($this->getContents($request));
41 3
        if (!($input instanceof \stdClass)) {
42 1
            throw new BadRequestException('Request content must be valid JSON object.');
43
        }
44
45 2
        return $this->normalizer->denormalize($input, UpdateType::class);
46
    }
47
48
    /**
49
     * @param $request
50
     *
51
     * @throws BadRequestException
52
     *
53
     * @return string
54
     */
55 3
    private function getContents($request): string
56
    {
57 3
        if ($request instanceof RequestInterface) {
58 2
            return $request->getBody()->getContents();
59
        }
60 1
        if (\is_string($request)) {
61 1
            return $request;
62
        }
63
        throw new BadRequestException('Request must be instance of Psr\Http\Message\RequestInterface or string.');
64
    }
65
}
66