SendSimple::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\PublicKeyStore;
14
use Threema\MsgApi\Receiver;
15
16
class SendSimple extends Base {
17
	/**
18
	 * @var PublicKeyStore
19
	 */
20
	private $publicKeyStore;
21
22
	/**
23
	 * @param PublicKeyStore $publicKeyStore
24
	 */
25
	public function __construct(PublicKeyStore $publicKeyStore) {
26
		parent::__construct('Send Simple Message',
27
			array(self::argThreemaId, self::argFrom, self::argSecret),
28
			'Send a message from standard input with server-side encryption to the given ID. is the API identity and \'secret\' is the API secret. the message ID on success.');
29
		$this->publicKeyStore = $publicKeyStore;
30
	}
31
32
	protected function doRun() {
33
		$to = $this->getArgument(self::argThreemaId);
34
		$from = $this->getArgument(self::argFrom);
35
		$secret = $this->getArgument(self::argSecret);
36
		Common::required($to, $from, $secret);
37
38
		$message = $this->readStdIn();
39
		if(strlen($message) === 0) {
40
			throw new \InvalidArgumentException('please define a message');
41
		}
42
43
		$settings = new ConnectionSettings(
44
			$from,
45
			$secret
46
		);
47
48
		$connector = new Connection($settings, $this->publicKeyStore);
49
		$receiver = new Receiver($to, Receiver::TYPE_ID);
50
51
		$result = $connector->sendSimple($receiver, $message);
52
		if($result->isSuccess()) {
53
			Common::l('Message ID: '.$result->getMessageId());
54
		}
55
		else {
56
			Common::l('Error: '.$result->getErrorMessage());
57
		}
58
	}
59
}
60