AbstractRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSignature() 0 3 1
A getTime() 0 12 3
A __construct() 0 4 1
A validateTimestamp() 0 3 1
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