ReceiveMessage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A doRun() 0 45 4
1
<?php
2
/**
3
 * @author Threema GmbH
4
 * @copyright Copyright (c) 2015-2016 Threema GmbH
5
 */
6
7
8
namespace Threema\Console\Command;
9
10
use Threema\Console\Common;
11
use Threema\MsgApi\Connection;
12
use Threema\MsgApi\ConnectionSettings;
13
use Threema\MsgApi\Helpers\E2EHelper;
14
use Threema\MsgApi\PublicKeyStore;
15
use Threema\MsgApi\Tools\CryptTool;
16
17
class ReceiveMessage extends Base {
18
	const argOutputFolder = 'outputFolder';
19
	const argMessageId = 'messageId';
20
21
	/**
22
	 * @var PublicKeyStore
23
	 */
24
	private $publicKeyStore;
25
26
	/**
27
	 * @param PublicKeyStore $publicKeyStore
28
	 */
29
	public function __construct(PublicKeyStore $publicKeyStore) {
30
		parent::__construct('Decrypt a Message and download the Files',
31
			array(self::argThreemaId, self::argFrom, self::argSecret, self::argPrivateKey, self::argMessageId, self::argNonce),
32
			'Decrypt a box (must be provided on stdin) message and download (if the message is an image or file message) the file(s) to the given <'.self::argOutputFolder.'> folder',
33
			array(self::argOutputFolder));
34
		$this->publicKeyStore = $publicKeyStore;
35
	}
36
37
	protected function doRun() {
38
		$cryptTool = CryptTool::getInstance();
39
40
		$sendersThreemaId = $this->getArgumentThreemaId(self::argThreemaId);
41
		$id = $this->getArgumentThreemaId(self::argFrom);
42
		$secret = $this->getArgument(self::argSecret);
43
		$privateKey = $this->getArgumentPrivateKey(self::argPrivateKey);
44
		$nonce = $cryptTool->hex2bin($this->getArgument(self::argNonce));
45
		$messageId = $this->getArgument(self::argMessageId);
46
		$outputFolder = $this->getArgument(self::argOutputFolder);
47
48
		$box = $cryptTool->hex2bin($this->readStdIn());
49
50
		Common::required($box, $id, $secret, $privateKey, $nonce);
51
52
		$settings = new ConnectionSettings(
53
			$id,
54
			$secret
55
		);
56
57
		$connector = new Connection($settings, $this->publicKeyStore);
58
		$helper = new E2EHelper($privateKey, $connector);
59
		$message = $helper->receiveMessage(
60
			$sendersThreemaId,
61
			$messageId,
62
			$box,
63
			$nonce,
64
			$outputFolder
65
		);
66
67
		if(null === $message) {
68
			Common::e('invalid message');
69
			return;
70
		}
71
72
		if($message->isSuccess()) {
73
			Common::l($message->getMessageId().' - '.$message->getThreemaMessage());
74
			foreach($message->getFiles() as $fileName => $filePath) {
75
				Common::l('   received file '.$fileName.' in '.$filePath);
76
			}
77
		}
78
		else {
79
			Common::e('Error: '.implode("\n", $message->getErrors()));
80
		}
81
	}
82
}
83