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
|
|
|
|
16
|
|
|
class SendE2EImage extends Base { |
17
|
|
|
const argImageFile = 'imageFile'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var PublicKeyStore |
21
|
|
|
*/ |
22
|
|
|
private $publicKeyStore; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param PublicKeyStore $publicKeyStore |
26
|
|
|
*/ |
27
|
|
|
public function __construct(PublicKeyStore $publicKeyStore) { |
28
|
|
|
parent::__construct('Send a End-to-End Encrypted Image Message', |
29
|
|
|
array(self::argThreemaId, self::argFrom, self::argSecret, self::argPrivateKey, self::argImageFile), |
30
|
|
|
'Encrypt the image file and send the message to the given ID. \'from\' is the API identity and \'secret\' is the API secret. Prints the message ID on success.'); |
31
|
|
|
$this->publicKeyStore = $publicKeyStore; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
protected function doRun() { |
|
|
|
|
35
|
|
|
$threemaId = $this->getArgument(self::argThreemaId); |
36
|
|
|
$from = $this->getArgument(self::argFrom); |
37
|
|
|
$secret = $this->getArgument(self::argSecret); |
38
|
|
|
$privateKey = $this->getArgumentPrivateKey(self::argPrivateKey); |
39
|
|
|
|
40
|
|
|
$path = $this->getArgumentFile(self::argImageFile); |
41
|
|
|
|
42
|
|
|
Common::required($threemaId, $from, $secret, $privateKey, $path); |
43
|
|
|
|
44
|
|
|
$settings = new ConnectionSettings( |
45
|
|
|
$from, |
46
|
|
|
$secret |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$connector = new Connection($settings, $this->publicKeyStore); |
50
|
|
|
|
51
|
|
|
$helper = new E2EHelper($privateKey, $connector); |
52
|
|
|
$result = $helper->sendImageMessage($threemaId, $path); |
53
|
|
|
|
54
|
|
|
if($result->isSuccess()) { |
55
|
|
|
Common::l('Message ID: '.$result->getMessageId()); |
56
|
|
|
} |
57
|
|
|
else { |
58
|
|
|
Common::e('Error: '.$result->getErrorMessage()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
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.