Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add type property to Media Class #224

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Message/Media/MediaType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Netflie\WhatsAppCloudApi\Message\Media;

use MyCLabs\Enum\Enum;

/**
* @method static MediaType STICKER()
* @method static MediaType IMAGE()
* @method static MediaType DOCUMENT()
* @method static MediaType AUDIO()
* @method static MediaType VIDEO()
* @method static MediaType VOICE()
*/
final class MediaType extends Enum
{
private const STICKER = 'sticker';

private const IMAGE = 'image';

private const DOCUMENT = 'document';

private const AUDIO = 'audio';

private const VIDEO = 'video';

private const VOICE = 'voice';
}
13 changes: 12 additions & 1 deletion src/WebHook/Notification/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Netflie\WhatsAppCloudApi\WebHook\Notification;

use Netflie\WhatsAppCloudApi\Message\Media\MediaType;

final class Media extends MessageNotification
{
private string $image_id;
Expand All @@ -14,6 +16,8 @@ final class Media extends MessageNotification

private string $caption;

private MediaType $type;

public function __construct(
string $id,
Support\Business $business,
Expand All @@ -22,7 +26,8 @@ public function __construct(
string $sha256,
string $filename,
string $caption,
string $received_at_timestamp
string $received_at_timestamp,
MediaType $type
) {
parent::__construct($id, $business, $received_at_timestamp);

Expand All @@ -31,6 +36,7 @@ public function __construct(
$this->sha256 = $sha256;
$this->filename = $filename;
$this->caption = $caption;
$this->type = $type;
}

public function imageId(): string
Expand All @@ -57,4 +63,9 @@ public function caption(): string
{
return $this->caption;
}

public function type(): MediaType
{
return $this->type;
}
}
5 changes: 4 additions & 1 deletion src/WebHook/Notification/MessageNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Netflie\WhatsAppCloudApi\WebHook\Notification;

use Netflie\WhatsAppCloudApi\Message\Media\MediaType;

class MessageNotificationFactory
{
public function buildFromPayload(array $metadata, array $message, array $contact): MessageNotification
Expand Down Expand Up @@ -43,7 +45,8 @@ private function buildMessageNotification(array $metadata, array $message): Mess
$message[$message['type']]['sha256'],
$message[$message['type']]['filename'] ?? '',
$message[$message['type']]['caption'] ?? '',
$message['timestamp']
$message['timestamp'],
new MediaType($message['type'])
);
case 'location':
return new Location(
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/WebHook/NotificationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Netflie\WhatsAppCloudApi\Tests\Unit\WebHook;

use Netflie\WhatsAppCloudApi\Message\Media\MediaType;
use Netflie\WhatsAppCloudApi\WebHook\Notification;
use Netflie\WhatsAppCloudApi\WebHook\NotificationFactory;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -331,6 +332,7 @@ public function test_build_from_payload_can_build_an_image_notification()
$this->assertEquals('IMAGE_HASH', $notification->sha256());
$this->assertEquals('image/jpeg', $notification->mimeType());
$this->assertEquals('CAPTION_TEXT', $notification->caption());
$this->assertEquals(MediaType::IMAGE(), $notification->type());
}

public function test_build_from_payload_can_build_an_document_notification()
Expand Down Expand Up @@ -379,6 +381,7 @@ public function test_build_from_payload_can_build_an_document_notification()
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $notification->mimeType());
$this->assertEquals('CAPTION_TEXT', $notification->caption());
$this->assertEquals('FILENAME', $notification->filename());
$this->assertEquals(MediaType::DOCUMENT(), $notification->type());
}

public function test_build_from_payload_can_build_a_sticker_notification()
Expand Down Expand Up @@ -431,6 +434,7 @@ public function test_build_from_payload_can_build_a_sticker_notification()
$this->assertEquals('STICKER_ID', $notification->imageId());
$this->assertEquals('STICKER_HASH', $notification->sha256());
$this->assertEquals('image/webp', $notification->mimeType());
$this->assertEquals(MediaType::STICKER(), $notification->type());
}

public function test_build_from_payload_can_build_an_unknown_notification()
Expand Down