Skip to content

Commit

Permalink
delete Receiver class and add Connection::sendToEmail(), sendToPhone(…
Browse files Browse the repository at this point in the history
…) etc
  • Loading branch information
pavarnos committed Aug 14, 2018
1 parent 6bf1a2f commit e3e1c08
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 135 deletions.
7 changes: 1 addition & 6 deletions samples/testSendSimple.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php
declare(strict_types=1);

use Threema\MsgApi\Receiver;

require_once '../vendor/autoload.php';

$factory = new \Threema\MsgApi\ConnectionFactory();
$connection = $factory->getConnection('*YOUR_GATEWAY_THREEMA_ID', 'YOUR_GATEWAY_THREEMA_ID_SECRET');

//create a receiver
$receiver = new Receiver('ECHOECHO', Receiver::TYPE_ID);

$result = $connection->sendSimple($receiver, "This is a Test Message");
$result = $connection->sendToThreemaID('ECHOECHO', "This is a Test Message");
if ($result->isSuccess()) {
echo 'Message ID: ' . $result->getMessageId() . "\n";
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/Threema/Console/Symfony/MessageSendSimpleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Receiver;

class MessageSendSimpleCommand extends AbstractNetworkedCommand
{
Expand All @@ -27,8 +26,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$receiver = new Receiver($this->getRecipientID($input), Receiver::TYPE_ID);
$result = $this->getConnection($input, $output)->sendSimple($receiver, $this->getMessage($input));
$result = $this->getConnection($input, $output)
->sendToThreemaID($this->getRecipientID($input), $this->getMessage($input));
$this->assertSuccess($result);
$output->writeln($result->getMessageId());
return 0;
Expand Down
44 changes: 27 additions & 17 deletions src/Threema/MsgApi/Commands/SendSimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,38 @@
namespace Threema\MsgApi\Commands;

use Threema\MsgApi\Commands\Results\SendSimpleResult;
use Threema\MsgApi\Receiver;

class SendSimple implements CommandInterface
{
/**
* @var string
*/
/** @var string */
private $text;

/**
* @var \Threema\MsgApi\Receiver
*/
private $receiver;
/** @var string */
private $to;

/**
* @param \Threema\MsgApi\Receiver $receiver
* @param string $text
*/
public function __construct(Receiver $receiver, $text)
/** @var string */
private $address;

protected function __construct(string $to, string $address, string $text)
{
$this->text = $text;
$this->to = $to;
$this->address = $address;
}

public static function toThreemaID(string $threemaID, string $text): self
{
return new self('to', strtoupper($threemaID), $text);
}

public static function toEmail(string $email, string $text): self
{
return new self('email', strtolower(trim($email)), $text);
}

public static function toPhoneNo(string $phoneNo, string $text): self
{
$this->text = $text;
$this->receiver = $receiver;
return new self('phone', preg_replace('/[^0-9]/', '', $phoneNo), $text);
}

/**
Expand All @@ -46,8 +56,8 @@ public function getText(): string
*/
public function getParams(): array
{
$p = $this->receiver->getParams();
$p['text'] = $this->getText();
$p[$this->to] = $this->address;
$p['text'] = $this->text;
return $p;
}

Expand Down
23 changes: 16 additions & 7 deletions src/Threema/MsgApi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ public function __construct(HttpDriverInterface $driver, AbstractEncryptor $encr
$this->encryptor = $encryptor;
}

/**
* @param Receiver $receiver
* @param string $text
* @return SendSimpleResult
*/
public function sendSimple(Receiver $receiver, string $text): SendSimpleResult
public function sendToThreemaID(string $threemaID, string $text): SendSimpleResult
{
$result = $this->driver->postForm(SendSimple::toThreemaID($threemaID, $text));
assert($result instanceof SendSimpleResult);
return $result;
}

public function sendToEmail(string $email, string $text): SendSimpleResult
{
$result = $this->driver->postForm(SendSimple::toEmail($email, $text));
assert($result instanceof SendSimpleResult);
return $result;
}

public function sendToPhoneNo(string $phoneNo, string $text): SendSimpleResult
{
$result = $this->driver->postForm(new SendSimple($receiver, $text));
$result = $this->driver->postForm(SendSimple::toPhoneNo($phoneNo, $text));
assert($result instanceof SendSimpleResult);
return $result;
}
Expand Down
102 changes: 0 additions & 102 deletions src/Threema/MsgApi/Receiver.php

This file was deleted.

0 comments on commit e3e1c08

Please sign in to comment.