-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9a44f0
commit 3e17c39
Showing
10 changed files
with
296 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inisiatif\WhatsappQontakPhp; | ||
|
||
use Http\Client\HttpClient; | ||
|
||
final class ClientFactory | ||
{ | ||
/** | ||
* @var Client|null | ||
*/ | ||
private static $client = null; | ||
|
||
/** | ||
* @var NullClient|null | ||
*/ | ||
private static $nullClient = null; | ||
|
||
public static function makeFromArray(array $config, HttpClient $httpClient = null): ClientInterface | ||
{ | ||
if (! self::$client instanceof Client) { | ||
self::$client = new Client( | ||
Credential::fromArray($config), | ||
$httpClient | ||
); | ||
} | ||
|
||
return self::$client; | ||
} | ||
|
||
public static function makeTestingClient(): ClientInterface | ||
{ | ||
if (! self::$nullClient instanceof NullClient) { | ||
self::$nullClient = new NullClient(); | ||
} | ||
|
||
return self::$nullClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inisiatif\WhatsappQontakPhp\Illuminate; | ||
|
||
use Inisiatif\WhatsappQontakPhp\Message\Message; | ||
|
||
final class Envelope | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $templateId; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $channelId; | ||
|
||
/** | ||
* @var Message | ||
*/ | ||
private $message; | ||
|
||
public function __construct(string $templateId, string $channelId, Message $message) | ||
{ | ||
$this->templateId = $templateId; | ||
$this->channelId = $channelId; | ||
$this->message = $message; | ||
} | ||
|
||
public function getTemplateId(): string | ||
{ | ||
return $this->templateId; | ||
} | ||
|
||
public function getChannelId(): string | ||
{ | ||
return $this->channelId; | ||
} | ||
|
||
public function getMessage(): Message | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inisiatif\WhatsappQontakPhp\Illuminate; | ||
|
||
use Inisiatif\WhatsappQontakPhp\ClientInterface; | ||
|
||
final class QontakChannel | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $client; | ||
|
||
public function __construct(ClientInterface $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* @param mixed $notifiable | ||
*/ | ||
public function send($notifiable, QontakNotification $notification): void | ||
{ | ||
$envelope = $notification->toQontak($notifiable); | ||
|
||
$this->client->send( | ||
$envelope->getTemplateId(), | ||
$envelope->getChannelId(), | ||
$envelope->getMessage() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inisiatif\WhatsappQontakPhp\Illuminate; | ||
|
||
interface QontakNotification | ||
{ | ||
/** | ||
* @param mixed $notifiable | ||
*/ | ||
public function toQontak($notifiable): Envelope; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inisiatif\WhatsappQontakPhp; | ||
|
||
use Inisiatif\WhatsappQontakPhp\Message\Message; | ||
|
||
final class NullClient implements ClientInterface | ||
{ | ||
public function send(string $templateId, string $channelId, Message $message): Response | ||
{ | ||
return new Response('messageId', $message->getReceiver()->getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inisiatif\WhatsappQontakPhp\Tests; | ||
|
||
use Mockery; | ||
use PHPUnit\Framework\TestCase; | ||
use Inisiatif\WhatsappQontakPhp\Client; | ||
use Inisiatif\WhatsappQontakPhp\NullClient; | ||
use Inisiatif\WhatsappQontakPhp\ClientFactory; | ||
use Http\Client\Common\HttpMethodsClientInterface; | ||
|
||
final class ClientFactoryTest extends TestCase | ||
{ | ||
protected function tearDown(): void | ||
{ | ||
Mockery::close(); | ||
} | ||
|
||
public function test_can_create_client(): void | ||
{ | ||
$config = [ | ||
'username' => 'username', | ||
'password' => 'password', | ||
'client_id' => 'clientId', | ||
'client_secret' => 'secret', | ||
]; | ||
|
||
$httpClient = Mockery::mock(HttpMethodsClientInterface::class)->makePartial(); | ||
|
||
$client = ClientFactory::makeFromArray($config, $httpClient); | ||
$this->assertInstanceOf(Client::class, $client); | ||
|
||
$client = ClientFactory::makeTestingClient(); | ||
$this->assertInstanceOf(NullClient::class, $client); | ||
} | ||
|
||
public function test_create_same_object_in_multiple_creation(): void | ||
{ | ||
$config = [ | ||
'username' => 'username', | ||
'password' => 'password', | ||
'client_id' => 'clientId', | ||
'client_secret' => 'secret', | ||
]; | ||
|
||
$httpClient = Mockery::mock(HttpMethodsClientInterface::class)->makePartial(); | ||
|
||
$client1 = ClientFactory::makeFromArray($config, $httpClient); | ||
$this->assertInstanceOf(Client::class, $client1); | ||
|
||
$client2 = ClientFactory::makeFromArray($config, $httpClient); | ||
$this->assertSame($client1, $client2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters