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

Automatic posting to Facebook #6

Merged
merged 1 commit into from Feb 2, 2018
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+99 −2
Diff settings

Always

Just for now

Copy path View file
@@ -4,13 +4,16 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
/**
* Class Blog
* @package App
*/
class Blog extends Model
{
use Notifiable;
/**
* @var string
*/
@@ -42,4 +45,9 @@ public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::parse($date);
}
public function getLink()
{
return route('app.blog.view', $this->slug);
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Listeners;
use App\Notifications\ArticlePublished;
/**
* Class BlogEventSubscriber
* @package App\Listeners
*/
class BlogEventSubscriber
{
/**
* Handle blog creating events.
*
* @param $blog
*/
public function onCreated($blog)
{
$blog->notify(new ArticlePublished());
}
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
$events->listen(
'eloquent.created: App\Blog',
'App\Listeners\BlogEventSubscriber@onCreated'
);
}
}
@@ -0,0 +1,38 @@
<?php
namespace App\Notifications;
use App\Blog;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\FacebookPoster\FacebookPosterChannel;
use NotificationChannels\FacebookPoster\FacebookPosterPost;
/**
* Class ArticlePublished
* @package App\Notifications
*/
class ArticlePublished extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [FacebookPosterChannel::class];
}
/**
* @param $blog
*/
public function toFacebookPoster($blog)
{
return with(new FacebookPosterPost($blog->title))
->withLink($blog->getLink());
}
}
@@ -2,8 +2,8 @@
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
@@ -18,6 +18,15 @@ class EventServiceProvider extends ServiceProvider
],
];
/**
* The subscriber classes to register.
*
* @var array
*/
protected $subscribe = [
'App\Listeners\BlogEventSubscriber',
];
/**
* Register any events for your application.
*
Copy path View file
@@ -8,6 +8,7 @@
"php": ">=7.0.0",
"dts/ebay-sdk-php": "^12.0",
"fideloper/proxy": "~3.3",
"laravel-notification-channels/facebook-poster": "^1.1",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"omnipay/paypal": "^2.6",
Copy path View file
@@ -12,7 +12,7 @@
| default location for this type of information, allowing packages
| to have a conventional place to find your various credentials.
|
*/
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
@@ -35,4 +35,9 @@
'secret' => env('STRIPE_SECRET'),
],
'facebook_poster' => [
'app_id' => env('FACEBOOK_APP_ID'),
'app_secret' => env('FACEBOOK_APP_SECRET'),
'access_token' => env('FACEBOOK_ACCESS_TOKEN'),
],
];
ProTip! Use n and p to navigate between commits in a pull request.