Common::getPublicKey()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 3
Ratio 50 %

Importance

Changes 0
Metric Value
dl 3
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
/**
3
 * @author Threema GmbH
4
 * @copyright Copyright (c) 2015-2016 Threema GmbH
5
 */
6
7
8
namespace Threema\Console;
9
10
use Threema\Core\Exception;
11
use Threema\MsgApi\Constants;
12
13
class Common {
14
	/**
15
	 * output a string, wrap at 100 chars
16
	 *
17
	 * @param string $string string to output
18
	 * @param int $indent indent
19
	 */
20
	public static function l($string = '', $indent = 0) {
21
		$pad = str_repeat('  ', $indent);
22
		echo $pad . wordwrap($string, 100, "\n" . $pad) . "\n";
23
	}
24
25
	/**
26
	 * output a line
27
	 *
28
	 * @param string $string string to output
29
	 */
30
	public static function ln($string = '') {
31
		echo $string . "\n";
32
	}
33
34
	/**
35
	 * output a error message to the stderr
36
	 *
37
	 * @param string $msg
38
	 */
39
	public static function e($msg) {
40
		$STDERR = fopen('php://stderr', 'w+');
41
		fwrite($STDERR, $msg."\n");
42
43
	}
44
45
	/**
46
	 * check arguments for null, throws an exception on null or strlen == 0
47
	 *
48
	 * @params ...$things
49
	 * @throws \Threema\Core\Exception
50
	 */
51
	public static function required() {
52
		$argCount = func_num_args();
53
		for($n = 0; $n < $argCount; $n++) {
54
			$o = func_get_arg($n);
55
			if(null === $o || (is_scalar($o) && strlen($o) == 0)) {
56
				throw new Exception('invalid data');
57
			}
58
		}
59
	}
60
61
	/**
62
	 * Append the prefix to the the PublicKey @key
63
	 *
64
	 * @param string $key PublicKey in hex
65
	 * @return null|string
66
	 */
67
	public static function convertPublicKey($key) {
68
		if(null !== $key) {
69
			return Constants::PUBLIC_KEY_PREFIX.$key;
70
		}
71
		return null;
72
	}
73
74
	/**
75
	 * Extract the PublicKey
76
	 *
77
	 * @param string $stringWithPrefix PublicKey in hex with the key-prefix
78
	 * @return null|string
79
	 */
80
	public static function getPublicKey($stringWithPrefix = null) {
81 View Code Duplication
		if(null !== $stringWithPrefix && substr($stringWithPrefix, 0, strlen(Constants::PUBLIC_KEY_PREFIX)) == Constants::PUBLIC_KEY_PREFIX) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
			return substr($stringWithPrefix, strlen(Constants::PUBLIC_KEY_PREFIX));
83
		}
84
		return null;
85
	}
86
87
	/**
88
	 * Append the prefix to the the PrivateKey @key
89
	 *
90
	 * @param string $key PrivateKey in hex
91
	 * @return null|string
92
	 */
93
	public static function convertPrivateKey($key) {
94
		if(null !== $key) {
95
			return Constants::PRIVATE_KEY_PREFIX.$key;
96
		}
97
		return null;
98
	}
99
100
	/**
101
	 * Extract the PrivateKey
102
	 *
103
	 * @param string $stringWithPrefix PrivateKey in hex with the key-prefix (@Constants::PRIVATE_KEY_PREFIX)
104
	 * @return null|string
105
	 */
106
	public static function getPrivateKey($stringWithPrefix = null) {
107 View Code Duplication
		if(null !== $stringWithPrefix && substr($stringWithPrefix, 0, strlen(Constants::PRIVATE_KEY_PREFIX)) == Constants::PRIVATE_KEY_PREFIX) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
			return substr($stringWithPrefix, strlen(Constants::PRIVATE_KEY_PREFIX));
109
		}
110
		return null;
111
	}
112
}
113