Skip to content

Commit

Permalink
2.x: Split responsability of Request class
Browse files Browse the repository at this point in the history
  • Loading branch information
aalbarca committed Jan 6, 2023
1 parent d2b3d72 commit 17f76e9
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 301 deletions.
12 changes: 6 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function __construct(string $graph_version, ?ClientHandler $handler = nul
*
* @throws Netflie\WhatsAppCloudApi\Response\ResponseException
*/
public function sendRequest(Request $request): Response
public function sendMessage(Request\MessageRequest $request): Response
{
$raw_response = $this->handler->send(
$this->buildRequestUri($request),
$request->encodedBody(),
$raw_response = $this->handler->postJsonData(
$this->buildRequestUri($request->nodePath()),
$request->body(),
$request->headers(),
$request->timeout()
);
Expand Down Expand Up @@ -75,8 +75,8 @@ private function buildBaseUri(): string
return self::BASE_GRAPH_URL . '/' . $this->graph_version;
}

private function buildRequestUri(Request $request): string
private function buildRequestUri(string $node_path): string
{
return $this->buildBaseUri() . '/' . $request->fromPhoneNumberId() . '/messages';
return $this->buildBaseUri() . '/' . $node_path;
}
}
6 changes: 3 additions & 3 deletions src/Http/ClientHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
interface ClientHandler
{
/**
* Sends a request to the server and returns the raw response.
* Sends a POST request to the server and returns the raw response.
*
* @param string $url The endpoint to send the request to.
* @param string $body The body of the request.
* @param array $body The body of the request.
* @param array $headers The request headers.
* @param int $timeout The timeout in seconds for the request.
*
* @return RawResponse Response from the server.
*
* @throws Netflie\WhatsAppCloudApi\Response\ResponseException
*/
public function send(string $url, string $body, array $headers, int $timeout): RawResponse;
public function postJsonData(string $url, array $body, array $headers, int $timeout): RawResponse;
}
4 changes: 2 additions & 2 deletions src/Http/GuzzleClientHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function __construct(?Client $guzzle_client = null)
* {@inheritDoc}
*
*/
public function send(string $url, string $body, array $headers, int $timeout): RawResponse
public function postJsonData(string $url, array $body, array $headers, int $timeout): RawResponse
{
$raw_handler_response = $this->guzzle_client->post($url, [
'body' => $body,
'json' => $body,
'headers' => $headers,
'timeout' => $timeout,
]);
Expand Down
81 changes: 1 addition & 80 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,18 @@

namespace Netflie\WhatsAppCloudApi;

use Netflie\WhatsAppCloudApi\Message\Message;

abstract class Request
{
/**
* @const int The timeout in seconds for a normal request.
*/
public const DEFAULT_REQUEST_TIMEOUT = 60;

/**
* @var Message WhatsApp Message to be sent.
*/
protected Message $message;

/**
* @var string The access token to use for this request.
*/
protected string $access_token;

/**
* @var string WhatsApp Number Id from messages will sent.
*/
protected string $from_phone_number_id;

/**
* The raw body request.
*
* @return array
*/
protected array $body;

/**
* The raw encoded body request.
*
* @return string
*/
protected string $encoded_body;

/**
* The timeout request.
*
Expand All @@ -53,35 +27,10 @@ abstract class Request
* @param Message $message
* @param string $access_token
*/
public function __construct(Message $message, string $access_token, string $from_phone_number_id, ?int $timeout = null)
public function __construct(string $access_token, ?int $timeout = null)
{
$this->message = $message;
$this->access_token = $access_token;
$this->from_phone_number_id = $from_phone_number_id;
$this->timeout = $timeout ?? static::DEFAULT_REQUEST_TIMEOUT;

$this->makeBody();
$this->encodeBody();
}

/**
* Returns the raw body of the request.
*
* @return array
*/
public function body(): array
{
return $this->body;
}

/**
* Returns the body of the request encoded.
*
* @return string
*/
public function encodedBody(): string
{
return $this->encoded_body;
}

/**
Expand All @@ -93,7 +42,6 @@ public function headers(): array
{
return [
'Authorization' => "Bearer $this->access_token",
'Content-Type' => 'application/json',
];
}

Expand All @@ -107,16 +55,6 @@ public function accessToken(): string
return $this->access_token;
}

/**
* Return WhatsApp Number Id for this request.
*
* @return string
*/
public function fromPhoneNumberId(): string
{
return $this->from_phone_number_id;
}

/**
* Return the timeout for this request.
*
Expand All @@ -126,21 +64,4 @@ public function timeout(): int
{
return $this->timeout;
}

/**
* Makes the raw body of the request.
*
* @return array
*/
abstract protected function makeBody(): void;

/**
* Encodes the raw body of the request.
*
* @return array
*/
private function encodeBody(): void
{
$this->encoded_body = json_encode($this->body());
}
}
54 changes: 54 additions & 0 deletions src/Request/MessageRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request;

use Netflie\WhatsAppCloudApi\Message\Message;
use Netflie\WhatsAppCloudApi\Request;

abstract class MessageRequest extends Request
{
/**
* @var Message WhatsApp Message to be sent.
*/
protected Message $message;

/**
* @var string WhatsApp Number Id from messages will sent.
*/
protected string $from_phone_number_id;

public function __construct(Message $message, string $access_token, string $from_phone_number_id, ?int $timeout = null)
{
$this->message = $message;
$this->from_phone_number_id = $from_phone_number_id;

parent::__construct($access_token, $timeout);
}

/**
* Returns the raw body of the request.
*
* @return array
*/
abstract public function body(): array;

/**
* Return WhatsApp Number Id for this request.
*
* @return string
*/
public function fromPhoneNumberId(): string
{
return $this->from_phone_number_id;
}

/**
* WhatsApp node path.
*
* @return string
*/
public function nodePath(): string
{
return $this->from_phone_number_id . '/messages';
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request;
namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request;
use Netflie\WhatsAppCloudApi\Request\MessageRequest;

class RequestAudioMessage extends Request
class RequestAudioMessage extends MessageRequest
{
/**
* Makes the raw body of the request.
*
*/
protected function makeBody(): void
* {@inheritdoc}
*/
public function body(): array
{
$this->body = [
return [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request;
namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request;
use Netflie\WhatsAppCloudApi\Request\MessageRequest;

class RequestContactMessage extends Request
class RequestContactMessage extends MessageRequest
{
/**
* Makes the raw body of the request.
*
*/
protected function makeBody(): void
* {@inheritdoc}
*/
public function body(): array
{
$message_type = $this->message->type();

$this->body = [
$body = [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
Expand All @@ -40,7 +39,9 @@ protected function makeBody(): void
$phone_array['wa_id'] = $phone->waId();
}

$this->body[$message_type][0]['phones'][] = $phone_array;
$body[$message_type][0]['phones'][] = $phone_array;
}

return $body;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request;
namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request;
use Netflie\WhatsAppCloudApi\Request\MessageRequest;

class RequestDocumentMessage extends Request
class RequestDocumentMessage extends MessageRequest
{
/**
* Makes the raw body of the request.
*
*/
protected function makeBody(): void
* {@inheritdoc}
*/
public function body(): array
{
$this->body = [
return [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request;
namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request;
use Netflie\WhatsAppCloudApi\Request\MessageRequest;

class RequestImageMessage extends Request
class RequestImageMessage extends MessageRequest
{
/**
* Makes the raw body of the request.
*
*/
protected function makeBody(): void
* {@inheritdoc}
*/
public function body(): array
{
$this->body = [
return [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

namespace Netflie\WhatsAppCloudApi\Request;
namespace Netflie\WhatsAppCloudApi\Request\MessageRequest;

use Netflie\WhatsAppCloudApi\Request;
use Netflie\WhatsAppCloudApi\Request\MessageRequest;

class RequestLocationMessage extends Request
class RequestLocationMessage extends MessageRequest
{
/**
* Makes the raw body of the request.
*
*/
protected function makeBody(): void
* {@inheritdoc}
*/
public function body(): array
{
$this->body = [
return [
'messaging_product' => $this->message->messagingProduct(),
'recipient_type' => $this->message->recipientType(),
'to' => $this->message->to(),
Expand Down
Loading

0 comments on commit 17f76e9

Please sign in to comment.