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

Decouple post and channel #36

Merged
merged 1 commit into from
Oct 4, 2019
Merged
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
46 changes: 6 additions & 40 deletions src/Attachments/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 [];
}
}
14 changes: 1 addition & 13 deletions src/Attachments/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
45 changes: 1 addition & 44 deletions src/Attachments/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
20 changes: 5 additions & 15 deletions src/FacebookPosterChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
52 changes: 25 additions & 27 deletions src/FacebookPosterPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -50,7 +42,7 @@ class FacebookPosterPost
*
* @var string
*/
protected $apiEndpoint = 'me/feed';
protected $endpoint = 'me/feed';

/**
* Create a new post instance.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -136,21 +132,31 @@ 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;
}

/**
* Build Facebook post request body.
*
* @return array
*/
public function getPostBody()
public function getBody()
{
$this->validate();

Expand All @@ -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);
}
Expand All @@ -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;
}

Expand Down
24 changes: 2 additions & 22 deletions tests/Attachments/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,18 @@ 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);
}

/** @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);
}
}
6 changes: 3 additions & 3 deletions tests/FacebookPosterPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function it_can_be_scheduled()

$post->scheduledFor(1234);

$result = $post->getPostBody();
$result = $post->getBody();

$this->assertEquals([
'message' => 'message',
Expand All @@ -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',
Expand All @@ -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',
Expand Down