SignatureDataFormatter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 57
rs 10
ccs 19
cts 22
cp 0.8636
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDataForSignature() 0 3 1
A __construct() 0 3 1
B generateMessage() 0 29 8
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Crypto;
4
5
use function array_key_exists;
6
use function array_merge;
7
use function implode;
8
use function is_array;
9
use function is_bool;
10
use function is_int;
11
12
class SignatureDataFormatter
13
{
14
15
	/** @var mixed[] */
16
	private $keysPriority;
17
18
	/**
19
	 * @param mixed[] $keysPriority
20
	 */
21 46
	public function __construct(array $keysPriority)
22
	{
23 46
		$this->keysPriority = $keysPriority;
24 46
	}
25
26
	/**
27
	 * @param mixed[] $data
28
	 * @return string
29
	 */
30 12
	public function formatDataForSignature(array $data): string
31
	{
32 12
		return implode('|', $this->generateMessage($data, $this->keysPriority));
33
	}
34
35
	/**
36
	 * @param mixed[] $data
37
	 * @param mixed[] $keys
38
	 * @return mixed[]
39
	 */
40 12
	private function generateMessage(array $data, array $keys): array
41
	{
42 12
		$message = [];
43
44 12
		foreach ($keys as $key => $values) {
45 8
			if (is_int($key)) {
46 3
				foreach ($data as $items) {
47 3
					$message = array_merge($message, $this->generateMessage($items, $values));
48
				}
49 3
				continue;
50
			}
51 8
			if (!array_key_exists($key, $data)) {
52 4
				continue;
53
			}
54
55 8
			if (is_array($values)) {
56 4
				$message = array_merge($message, $this->generateMessage($data[$key], $values));
57
			} else {
58 8
				if (is_bool($data[$key])) {
59
					$message[] = $data[$key]
60
						? 'true'
61
						: 'false';
62
				} else {
63 8
					$message[] = $data[$key];
64
				}
65
			}
66
		}
67
68 12
		return $message;
69
	}
70
71
}
72