Passed
Push — master ( f4ebb6...c31285 )
by Gaël
11:14
created

AbstractRequest::generateFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
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
    /** @var string */
13
    const REQUEST_URI = '';
14
15
    /**
16
     * @param string $securityKey
17
     * @param array $fields
18
     * @return string
19
     */
20
    public function generateSeal(string $securityKey, array $fields): string
21
    {
22
        ksort($fields);
23
24
        $query = http_build_query($fields, null, '*');
25
        $query = urldecode($query);
26
27
        return strtoupper(hash_hmac(
28
            'sha1',
29
            $query,
30
            $securityKey
31
        ));
32
    }
33
34
    /**
35
     * @param string $seal
36
     * @param array $fields
37
     * @return array
38
     */
39
    public function generateFields(string $seal, array $fields): array
40
    {
41
        return array_merge(
42
            $fields,
43
            ['MAC' => $seal]
44
        );
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    protected function getRequestUrl(): string
51
    {
52
        return self::REQUEST_URL;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    protected function getRequestUri(): string
59
    {
60
        return self::REQUEST_URI;
61
    }
62
63
    /**
64
     * @param bool $testMode
65
     * @return string
66
     */
67
    public function getUrl(bool $testMode = false): string
68
    {
69
        $requestUrl = $this->getRequestUrl();
70
        if ($testMode) {
71
            $requestUrl .= '/test';
72
        }
73
74
        return $requestUrl . '/' . $this->getRequestUri();
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    abstract public function validate(): bool;
81
82
    /**
83
     * @param string $eptCode
84
     * @param string $companyCode
85
     * @param string $version
86
     * @return array
87
     */
88
    abstract public function fieldsToArray(string $eptCode, string $companyCode, string $version): array;
89
}
90