From 00a989af88a32b48107f7535c597b8eff4ba51cd Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Fri, 4 Oct 2019 11:06:42 +1000 Subject: [PATCH] Decouple post and channel --- src/Attachments/Attachment.php | 46 ++++------------------------ src/Attachments/Image.php | 14 +-------- src/Attachments/Video.php | 45 +-------------------------- src/FacebookPosterChannel.php | 20 +++--------- src/FacebookPosterPost.php | 52 +++++++++++++++----------------- tests/Attachments/ImageTest.php | 24 ++------------- tests/FacebookPosterPostTest.php | 6 ++-- 7 files changed, 43 insertions(+), 164 deletions(-) diff --git a/src/Attachments/Attachment.php b/src/Attachments/Attachment.php index 878f662..81e7424 100644 --- a/src/Attachments/Attachment.php +++ b/src/Attachments/Attachment.php @@ -12,18 +12,14 @@ abstract class Attachment protected $path; /** - * The attachment API endpoint. + * Create a new attachment instance. * - * @var string + * @param string $path */ - protected $apiEndpoint; - - /** - * The attachment API method. - * - * @var string - */ - protected $apiMethod = null; + public function __construct($path) + { + $this->path = $path; + } /** * Get the attachment path. @@ -34,34 +30,4 @@ public function getPath() { return $this->path; } - - /** - * Get the attachment API endpoint. - * - * @return string - */ - public function getApiEndpoint() - { - return $this->apiEndpoint; - } - - /** - * Get the attachment API method. - * - * @return string - */ - public function getApiMethod() - { - return $this->apiMethod; - } - - /** - * Get additional attachment data. - * - * @return array - */ - public function getData() - { - return []; - } } diff --git a/src/Attachments/Image.php b/src/Attachments/Image.php index 8f32794..21395da 100644 --- a/src/Attachments/Image.php +++ b/src/Attachments/Image.php @@ -9,17 +9,5 @@ class Image extends Attachment * * @var string */ - protected $apiMethod = 'fileToUpload'; - - /** - * Create a new image instance. - * - * @param string $path - * @param string $apiEndpoint - */ - public function __construct($path, $apiEndpoint) - { - $this->path = $path; - $this->apiEndpoint = $apiEndpoint; - } + const API_METHOD = 'fileToUpload'; } diff --git a/src/Attachments/Video.php b/src/Attachments/Video.php index a0fc507..4a7a84c 100644 --- a/src/Attachments/Video.php +++ b/src/Attachments/Video.php @@ -4,53 +4,10 @@ class Video extends Attachment { - /** - * The video title. - * - * @var string - */ - protected $title; - - /** - * The video description. - * - * @var string - */ - protected $description; - /** * The video API method. * * @var string */ - protected $apiMethod = 'videoToUpload'; - - /** - * Create a new video instance. - * - * @param string $path - * @param string $title - * @param string $description - * @param string $apiEndpoint - */ - public function __construct($path, $title, $description, $apiEndpoint) - { - $this->path = $path; - $this->title = $title; - $this->description = $description; - $this->apiEndpoint = $apiEndpoint; - } - - /** - * Get additional attachment data. - * - * @return array - */ - public function getData() - { - return array_filter([ - 'title' => $this->title, - 'description' => $this->description, - ]); - } + const API_METHOD = 'videoToUpload'; } diff --git a/src/FacebookPosterChannel.php b/src/FacebookPosterChannel.php index f94bd29..0404ec5 100644 --- a/src/FacebookPosterChannel.php +++ b/src/FacebookPosterChannel.php @@ -36,25 +36,15 @@ public function send($notifiable, Notification $notification) $this->switchSettings($facebookSettings); } - $facebookMessage = $notification->toFacebookPoster($notifiable); + $post = $notification->toFacebookPoster($notifiable); - $postBody = $facebookMessage->getPostBody(); + $body = $post->getBody(); - $endpoint = $facebookMessage->getApiEndpoint(); - - if (isset($postBody['media'])) { - $endpoint = $postBody['media']->getApiEndpoint(); - - $postBody = array_merge($postBody, $postBody['media']->getData()); - - $method = $postBody['media']->getApiMethod(); - - $postBody['source'] = $this->facebook->{$method}($postBody['media']->getPath()); - - unset($postBody['media']); + if ($media = $post->getMedia()) { + $body['source'] = $this->facebook->{$media::API_METHOD}($media->getPath()); } - $this->facebook->post($endpoint, $postBody); + $this->facebook->post($post->getEndpoint(), $body); } /** diff --git a/src/FacebookPosterPost.php b/src/FacebookPosterPost.php index ed593a7..db1bfda 100644 --- a/src/FacebookPosterPost.php +++ b/src/FacebookPosterPost.php @@ -3,7 +3,6 @@ namespace NotificationChannels\FacebookPoster; use DateTimeInterface; -use NotificationChannels\FacebookPoster\Attachments\Link; use NotificationChannels\FacebookPoster\Attachments\Image; use NotificationChannels\FacebookPoster\Attachments\Video; use NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent; @@ -25,18 +24,11 @@ class FacebookPosterPost protected $link; /** - * The post image. + * The post media. * - * @var \NotificationChannels\FacebookPoster\Attachments\Image + * @var \NotificationChannels\FacebookPoster\Attachments\Attachment */ - protected $image; - - /** - * The post video. - * - * @var \NotificationChannels\FacebookPoster\Attachments\Video - */ - protected $video; + protected $media; /** * Additional post parameters. @@ -50,7 +42,7 @@ class FacebookPosterPost * * @var string */ - protected $apiEndpoint = 'me/feed'; + protected $endpoint = 'me/feed'; /** * Create a new post instance. @@ -96,7 +88,8 @@ public function withLink($link) */ public function withImage($path, $endpoint = 'me/photos') { - $this->image = new Image($path, $endpoint); + $this->media = new Image($path); + $this->endpoint = $endpoint; return $this; } @@ -112,7 +105,10 @@ public function withImage($path, $endpoint = 'me/photos') */ public function withVideo($path, $title = null, $description = null, $endpoint = 'me/videos') { - $this->video = new Video($path, $title, $description, $endpoint); + $this->media = new Video($path); + $this->params['title'] = $title; + $this->params['description'] = $description; + $this->endpoint = $endpoint; return $this; } @@ -136,13 +132,23 @@ public function scheduledFor($timestamp) } /** - * Return Facebook Post api endpoint. + * Return Facebook API endpoint. * * @return string */ - public function getApiEndpoint() + public function getEndpoint() + { + return $this->endpoint; + } + + /** + * Get the media attached to the post. + * + * @return \NotificationChannels\FacebookPoster\Attachment + */ + public function getMedia() { - return $this->apiEndpoint; + return $this->media; } /** @@ -150,7 +156,7 @@ public function getApiEndpoint() * * @return array */ - public function getPostBody() + public function getBody() { $this->validate(); @@ -162,14 +168,6 @@ public function getPostBody() $body['link'] = $this->link; } - if ($this->image != null) { - $body['media'] = $this->image; - } - - if ($this->video != null) { - $body['media'] = $this->video; - } - if ($this->params != null) { $body = array_merge($body, $this->params); } @@ -184,7 +182,7 @@ public function getPostBody() */ protected function validate() { - if ($this->message || $this->link || $this->image || $this->video) { + if ($this->message || $this->link || $this->media) { return; } diff --git a/tests/Attachments/ImageTest.php b/tests/Attachments/ImageTest.php index 3c3e937..74be8ac 100644 --- a/tests/Attachments/ImageTest.php +++ b/tests/Attachments/ImageTest.php @@ -10,7 +10,7 @@ class ImageTest extends TestCase /** @test */ public function it_can_be_instantiated() { - $image = new Image('path', 'endpoint'); + $image = new Image('path'); $this->assertInstanceOf(Image::class, $image); } @@ -18,30 +18,10 @@ public function it_can_be_instantiated() /** @test */ public function it_returns_given_path() { - $image = new Image('path', 'endpoint'); + $image = new Image('path'); $result = $image->getPath(); $this->assertEquals('path', $result); } - - /** @test */ - public function it_returns_given_endpoint() - { - $image = new Image('path', 'endpoint'); - - $result = $image->getApiEndpoint(); - - $this->assertEquals('endpoint', $result); - } - - /** @test */ - public function it_returns_method() - { - $image = new Image('path', 'endpoint'); - - $result = $image->getApiMethod(); - - $this->assertEquals('fileToUpload', $result); - } } diff --git a/tests/FacebookPosterPostTest.php b/tests/FacebookPosterPostTest.php index acde9e6..3beeae1 100644 --- a/tests/FacebookPosterPostTest.php +++ b/tests/FacebookPosterPostTest.php @@ -15,7 +15,7 @@ public function it_can_be_scheduled() $post->scheduledFor(1234); - $result = $post->getPostBody(); + $result = $post->getBody(); $this->assertEquals([ 'message' => 'message', @@ -31,7 +31,7 @@ public function it_can_be_scheduled_with_datetime() $post->scheduledFor(new DateTime('2000-01-01')); - $result = $post->getPostBody(); + $result = $post->getBody(); $this->assertEquals([ 'message' => 'message', @@ -47,7 +47,7 @@ public function it_can_be_scheduled_with_immutable_datetime() $post->scheduledFor(new DateTimeImmutable('2000-01-01')); - $result = $post->getPostBody(); + $result = $post->getBody(); $this->assertEquals([ 'message' => 'message',