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

Automatically post on facebook page #30

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_SECRET=

FB_APP_ID=
FB_APP_SECRET=
FB_PAGE_TOKEN=
21 changes: 17 additions & 4 deletions app/Http/Controllers/NeedsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Validator;
use App\Services\FacebookNotification;
use DomainException;

class NeedsController extends Controller
{
private $need;

private $fbNotifier;

/**
* NeedsController constructor.
* @param NeedsRepository $needsRepository
* @param FacebookNotification $fbNotifier
*/
public function __construct(NeedsRepository $needsRepository)
public function __construct(NeedsRepository $needsRepository, FacebookNotification $fbNotifier)
{
$this->need = $needsRepository;
$this->fbNotifier = $fbNotifier;
}

/**
Expand Down Expand Up @@ -63,12 +69,19 @@ public function save(Request $request)
->with('errors', $validator->errors()->all())
->withInput();
} else {
$response = $this->need->addNeed($request->all());
if ($response) {
try {

$need = $this->need->addNeed($request->all());

//need is being created first and if fb posting fail rest of the logic
//is executed
$fbId = $this->fbNotifier->publishNeed($need);
$this->need->updateNeed($need->id, ['fb_post_id' => $fbId]);

return redirect('/needs')
->with('isSuccess', true)
->with('message', 'Needs added.');
} else {
} catch (DomainException $e) {
return redirect('/needs/add')
->with('isSuccess', false)
->with('errors', ['Needs adding failed. Please try again.'])
Expand Down
2 changes: 1 addition & 1 deletion app/Need.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Need extends Model
* @var array
*/
protected $fillable = [
'name', 'telephone', 'address', 'city', 'needs', 'heads'
'name', 'telephone', 'address', 'city', 'needs', 'heads', 'fb_post_id'
];
}
17 changes: 14 additions & 3 deletions app/Repositories/NeedsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Need;
use App\Repositories\Contracts\NeedsInterface;
use Illuminate\Support\Facades\Log;
use DomainException;

class NeedsRepository implements NeedsInterface
{
Expand All @@ -23,10 +24,20 @@ class NeedsRepository implements NeedsInterface
public function addNeed($input)
{
try {
Need::create($input);
return true;
return Need::create($input);
} catch (\Exception $e) {
Log::error($e->getMessage());
throw new DomainException('Unable to create need.');
}
}

public function updateNeed($id, array $input)
{
$need = $this->findNeed($id);

if ($need) {
$need->fill($input);
return $need->save();
} else {
return false;
}
}
Expand Down
51 changes: 51 additions & 0 deletions app/Services/FacebookNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace App\Services;

use App\Need;
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use DomainException;
use Config;
use Log;

class FacebookNotification
{
protected $fb;

public function __construct()
{
//TODO: should come from the IoC container
//hardcoding to validate the feature
$this->fb = new Facebook([
'app_id' => Config::get('services.facebook.app_id'),
'app_secret' => Config::get('services.facebook.app_secret'),
'default_graph_version' => 'v2.9',
'default_access_token' => Config::get('services.facebook.page_token')
]);
}

public function publishNeed(Need $need)
{
$content = [];
$content[] = $need->needs;
$content[] = $need->address;
$content[] = $need->city;
$content[] = "People - ". $need->heads;
$content[] = $need->name.' - '.$need->telephone;

try {

$response = $this->fb->post('/me/feed', ['message' => implode($content, "\n")]);
$response->decodeBody();
return array_get($response->getDecodedBody(), 'id');

} catch(FacebookResponseException $e) {
Log::error($e);
return false;
} catch(FacebookSDKException $e) {
Log::error($e);
return false;
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"facebook/graph-sdk": "^5.5",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0"
},
Expand Down
6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
'secret' => env('STRIPE_SECRET'),
],

'facebook' => [
'app_id' => env('FB_APP_ID'),
'app_secret' => env('FB_APP_SECRET'),
'page_token' => env('FB_PAGE_TOKEN')
]

];
32 changes: 32 additions & 0 deletions database/migrations/2017_05_29_003241_add_fb_post_id_to_needs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddFbPostIdToNeeds extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('needs', function (Blueprint $table) {
$table->string("fb_post_id", 100)->after("heads")->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('needs', function (Blueprint $table) {
$table->dropColumn("needs");
});
}
}