Completed
Push — master ( 978af9...e20e57 )
by Jan
96:28 queued 67:16
created

SignatureDataFormatter::generateMessage()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.3518

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 17
cp 0.8235
rs 8.1954
c 0
b 0
f 0
cc 8
nc 8
nop 2
crap 8.3518
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 43
	public function __construct(array $keysPriority)
22
	{
23 43
		$this->keysPriority = $keysPriority;
24 43
	}
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