DeliveryReceipt::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * @author Threema GmbH
4
 * @copyright Copyright (c) 2015-2016 Threema GmbH
5
 */
6
7
8
namespace Threema\MsgApi\Messages;
9
10
use Threema\MsgApi\Tools\CryptTool;
11
12
class DeliveryReceipt extends ThreemaMessage {
13
	const TYPE_CODE = 0x80;
14
15
	/**
16
	 * map type => text
17
	 *
18
	 * @var array
19
	 */
20
	private static $receiptTypesToNames = array(
21
		1 => 'received',
22
		2 => 'read',
23
		3 => 'userack',
24
		4 => 'userdec');
25
26
	/**
27
	 * the type of this receipt
28
	 * @var int
29
	 */
30
	private $receiptType;
31
32
	/**
33
	 * list of message IDs acknowledged by this delivery receipt
34
	 * @var string[]
35
	 */
36
	private $ackedMessageIds;
37
38
	/**
39
	 * create instance
40
	 * @param int $receiptType the type of this receipt
41
	 * @param array $ackedMessageIds list of message IDs acknowledged by this delivery receipt
42
	 */
43
	public function __construct($receiptType, array $ackedMessageIds) {
44
		$this->receiptType = $receiptType;
45
		$this->ackedMessageIds = $ackedMessageIds;
46
	}
47
48
	/**
49
	 * Get the type of this delivery receipt as a numeric code (e.g. 1, 2, 3).
50
	 *
51
	 * @return int
52
	 */
53
	public function getReceiptType() {
54
		return $this->receiptType;
55
	}
56
57
	/**
58
	 * Get the type of this delivery receipt as a string (e.g. 'received', 'read', 'userack').
59
	 *
60
	 * @return string
61
	 */
62
	public function getReceiptTypeName() {
63
		if(true === array_key_exists($this->receiptType, self::$receiptTypesToNames)) {
64
			return self::$receiptTypesToNames[$this->receiptType];
65
		}
66
		return null;
67
	}
68
69
	/**
70
	 * Get the acknowledged message ids
71
	 * @return array
72
	 */
73
	public function getAckedMessageIds() {
74
		return $this->ackedMessageIds;
75
	}
76
77
	/**
78
	 * Convert to string
79
	 *
80
	 * @return string
81
	 */
82
	public function __toString() {
83
		$cryptTool = CryptTool::getInstance();
84
		$str = "Delivery receipt (" . $this->getReceiptTypeName() . "): ";
85
		$hexMessageIds = array();
86
		foreach ($this->ackedMessageIds as $messageId) {
87
			$hexMessageIds[] = $cryptTool->bin2hex($messageId);
88
		}
89
		$str .= join(", ", $hexMessageIds);
90
		return $str;
91
	}
92
93
	/**
94
	 * Get the message type code of this message.
95
	 *
96
	 * @return int message type code
97
	 */
98
	public final function getTypeCode() {
99
		return self::TYPE_CODE;
100
	}
101
}
102