Code Duplication    Length = 38-41 lines in 3 locations

source/Threema/Console/Command/LookupIdByEmail.php 1 location

@@ 15-55 (lines=41) @@
12
use Threema\MsgApi\ConnectionSettings;
13
use Threema\MsgApi\PublicKeyStore;
14
15
class LookupIdByEmail extends Base {
16
	const argEmail = 'email';
17
18
	/**
19
	 * @var PublicKeyStore
20
	 */
21
	private $publicKeyStore;
22
23
	/**
24
	 * @param PublicKeyStore $publicKeyStore
25
	 */
26
	public function __construct(PublicKeyStore $publicKeyStore) {
27
		parent::__construct('ID-Lookup By Email Address',
28
			array(self::argEmail, self::argFrom, self::argSecret),
29
			'Lookup the ID linked to the given email address (will be hashed locally).');
30
		$this->publicKeyStore = $publicKeyStore;
31
	}
32
33
	protected function doRun() {
34
		$email = $this->getArgument(self::argEmail);
35
		$from = $this->getArgumentThreemaId(self::argFrom);
36
		$secret = $this->getArgument(self::argSecret);
37
38
		Common::required($email, $from, $secret);
39
40
		//define connection settings
41
		$settings = new ConnectionSettings($from, $secret);
42
43
		//create a connection
44
		$connector = new Connection($settings, $this->publicKeyStore);
45
46
		$result = $connector->keyLookupByEmail($email);;
47
		Common::required($result);
48
		if($result->isSuccess()) {
49
			Common::l($result->getId());
50
		}
51
		else {
52
			Common::e($result->getErrorMessage());
53
		}
54
	}
55
}
56

source/Threema/Console/Command/LookupIdByPhoneNo.php 1 location

@@ 15-55 (lines=41) @@
12
use Threema\MsgApi\ConnectionSettings;
13
use Threema\MsgApi\PublicKeyStore;
14
15
class LookupIdByPhoneNo extends Base {
16
	const argPhoneNo = 'phoneNo';
17
18
	/**
19
	 * @var PublicKeyStore
20
	 */
21
	private $publicKeyStore;
22
23
	/**
24
	 * @param PublicKeyStore $publicKeyStore
25
	 */
26
	public function __construct(PublicKeyStore $publicKeyStore) {
27
		parent::__construct('ID-Lookup By Phone Number',
28
			array(self::argPhoneNo, self::argFrom, self::argSecret),
29
			'Lookup the ID linked to the given phone number (will be hashed locally).');
30
		$this->publicKeyStore = $publicKeyStore;
31
	}
32
33
	protected function doRun() {
34
		$phoneNo = $this->getArgument(self::argPhoneNo);
35
		$from = $this->getArgumentThreemaId(self::argFrom);
36
		$secret = $this->getArgument(self::argSecret);
37
38
		Common::required($phoneNo, $from, $secret);
39
40
		//define connection settings
41
		$settings = new ConnectionSettings($from, $secret);
42
43
		//create a connection
44
		$connector = new Connection($settings, $this->publicKeyStore);
45
46
		$result = $connector->keyLookupByPhoneNumber($phoneNo);;
47
		Common::required($result);
48
		if($result->isSuccess()) {
49
			Common::l($result->getId());
50
		}
51
		else {
52
			Common::e($result->getErrorMessage());
53
		}
54
	}
55
}
56

source/Threema/Console/Command/LookupPublicKeyById.php 1 location

@@ 15-52 (lines=38) @@
12
use Threema\MsgApi\ConnectionSettings;
13
use Threema\MsgApi\PublicKeyStore;
14
15
class LookupPublicKeyById extends Base {
16
	/**
17
	 * @var PublicKeyStore
18
	 */
19
	private $publicKeyStore;
20
21
	/**
22
	 * @param PublicKeyStore $publicKeyStore
23
	 */
24
	public function __construct(PublicKeyStore $publicKeyStore) {
25
		parent::__construct('Fetch Public Key',
26
			array(self::argThreemaId, self::argFrom, self::argSecret),
27
			'Lookup the public key for the given ID.');
28
		$this->publicKeyStore = $publicKeyStore;
29
	}
30
31
	protected function doRun() {
32
		$id = $this->getArgumentThreemaId(self::argThreemaId);
33
		$from = $this->getArgumentThreemaId(self::argFrom);
34
		$secret = $this->getArgument(self::argSecret);
35
36
		Common::required($id, $from, $secret);
37
38
		//define connection settings
39
		$settings = new ConnectionSettings($from, $secret);
40
41
		//create a connection
42
		$connector = new Connection($settings, $this->publicKeyStore);
43
44
		$result = $connector->fetchPublicKey($id);
45
		if($result->isSuccess()) {
46
			Common::l(Common::convertPublicKey($result->getPublicKey()));
47
		}
48
		else {
49
			Common::e($result->getErrorMessage());
50
		}
51
	}
52
}
53