AbstractRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 1
b 0
f 0
dl 0
loc 76
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 8 2
A generateSeal() 0 11 1
A getRequestUrl() 0 3 1
A generateFields() 0 5 1
1
<?php
2
3
namespace DansMaCulotte\Monetico\Requests;
4
5
use DansMaCulotte\Monetico\Monetico;
6
7
abstract class AbstractRequest
8
{
9
    /** @var string */
10
    const REQUEST_URL = Monetico::MAIN_REQUEST_URL;
11
12
    /**
13
     * @param string $securityKey
14
     * @param array $fields
15
     * @return string
16
     */
17
    public function generateSeal(string $securityKey, array $fields): string
18
    {
19
        ksort($fields);
20
21
        $query = http_build_query($fields, null, '*');
22
        $query = urldecode($query);
23
24
        return strtoupper(hash_hmac(
25
            'sha1',
26
            $query,
27
            $securityKey
28
        ));
29
    }
30
31
    /**
32
     * @param string $seal
33
     * @param array $fields
34
     * @return array
35
     */
36
    public function generateFields(string $seal, array $fields): array
37
    {
38
        return array_merge(
39
            $fields,
40
            ['MAC' => $seal]
41
        );
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    protected static function getRequestUrl(): string
48
    {
49
        return self::REQUEST_URL;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    abstract protected static function getRequestUri(): string;
56
57
    /**
58
     * @param bool $testMode
59
     * @return string
60
     */
61
    public static function getUrl(bool $testMode = false): string
62
    {
63
        $requestUrl = self::getRequestUrl();
64
        if ($testMode) {
65
            $requestUrl .= '/test';
66
        }
67
68
        return $requestUrl . '/' . static::getRequestUri();
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    abstract public function validate(): bool;
75
76
    /**
77
     * @param string $eptCode
78
     * @param string $companyCode
79
     * @param string $version
80
     * @return array
81
     */
82
    abstract public function fieldsToArray(string $eptCode, string $companyCode, string $version): array;
83
}
84