AbstractRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Request\Request;
6
7
abstract class AbstractRequest
8
{
9
    /**
10
     * @param string $type Request type
11
     * @param ?\DateTime $timestamp Request timestamp
12
     */
13 62
    public function __construct(
14
        public string $type,
15
        public ?\DateTime $timestamp = null,
16
    ) {
17 62
    }
18
19
    abstract public static function fromAmazonRequest(array $amazonRequest): self;
20
21 10
    public function validateTimestamp(): bool
22
    {
23 10
        return true;
24
    }
25
26 54
    public function validateSignature(): bool
27
    {
28 54
        return true;
29
    }
30
31 48
    protected static function getTime(string|int|null $value): ?\DateTime
32
    {
33 48
        if ($value !== null) {
34
            // Workaround for amazon developer console sending unix timestamp
35
            try {
36 48
                return new \DateTime((string) $value);
37 14
            } catch (\Exception $e) {
38 14
                return (new \DateTime())->setTimestamp((int) ((string) ($value / 1000)));
39
            }
40
        }
41
42 6
        return null;
43
    }
44
}
45