Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 3 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
101 changes: 101 additions & 0 deletions Models/streamPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/*
* This file is part of FOSFacebookBundle.
*
* (c) Teemu Reibacka <[email protected]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the license header should be the FOS one. Authorship for classes is kept through the @author annotation in the phpdoc of the class

*
* This class functions as a model for messages to be sent by the facebookPush-service to the Facebook api.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be in the phpdoc of the class, not in the license header (the license header should be the same than in other files)

*/

namespace FOS\FacebookBundle\Models;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a singular to be consistent with the Symfony naming convention


class streamPost
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please uppercase the first letter of the class name

{
protected $accessToken;
protected $message;
protected $attachment;

/**
* Sets the access token used in message body. This is set automatically inside facebookPush-service.
* @param string $accessToken
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
}
/**
*
* Sets the actual text content of the message
* @param string $message
*/
public function setMessage($message)
{
$this->message = $message;
}

/**
* Adds an attachment-picture to your Facebook message. If you specify a link, the image will function as a link.
* @param string $name This is the title of the attachment (top-most descriptive text)
* @param string $caption This is the image caption (text under the title)
* @param string $uriAttachment Full uri to the image you wish to attach. This location must be visible to Facebook, they will cache the picture.
* @param string $uriLinkOut Full uri to where you sish the image to link to
* @param string $description Description field in the message. If you do not specify this, Facebook may crawl the website specified in the link/image and attach a meta-description foiund from there to the message.
*/
public function setAttachment($name, $caption, $uriAttachment, $uriLinkOut = null, $description = null)
{
$attachment = array(
'name' => $name,
'caption' => $caption,
'picture' => $uriAttachment,
);

if (!empty($description)) {
$attachment['description'] = $description;
}
if (!empty($uriLinkOut)) {
$attachment['link'] = $uriLinkOut;
}
$this->attachment = $attachment;
}
/**
* Shortcut for adding a link to you FAcebook-message without an image. The setAttachment(...) will override this function.
* @param string $name This is the title of the link (top-most descriptive text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the phpdoc standard, you need to add a line between the short description and the tags

* @param string $linkOut Full uri to where you wish to link to
* @param string $caption Optional description of the link
*/
public function setLink($name, $linkOut, $caption = null)
{
$this->linkout = array(
'name' => $name,
'link' => $linkOut
);
if (!empty($caption)) $this->linkout['caption'] = $caption;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please always use curly braces even when there is only one line

}
/**
* Formats this object into on array that can be fed to the php-api class. This function will be called automatically.
* @throws \Exception
* @return array
*/
public function formatData()
{
if (empty($this->accessToken)) {
throw new \Exception('cannot format Facebook message: Facebook access token is empty');
}
if (empty($this->message)) {
throw new \Exception('cannot format Facebook message: Message is empty, nothing to send');
}
$stream_post = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use camel-cased variables

'access_token' => $this->accessToken,
'message' => $this->message
);
if (!empty($this->linkout)) {
$stream_post = array_merge($stream_post, $this->linkout);
}
if (!empty($this->attachment)) {
$stream_post = array_merge($stream_post, $this->attachment);
}

return $stream_post;
}
}
?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the closing PHP tag

4 changes: 4 additions & 0 deletions Resources/config/facebook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<argument type="service" id="service_container" />
<tag name="twig.extension" />
</service>

<service id="fos_facebook.facebook_push" class="FOS\FacebookBundle\Services\facebookPush">
<argument type="service" id="fos_facebook.api" />
</service>

</services>
</container>
158 changes: 158 additions & 0 deletions Services/facebookPush.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/*
* This file is part of FOSFacebookBundle.
*
* (c) Teemu Reibacka <[email protected]>
*
* File provides a service that abstracts Facebook API functionality such as retrieving user information and
* posting data to Facebook API.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here than in the other file

*/
namespace FOS\FacebookBundle\Services;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\FacebookBundle\Models\streamPost as BaseMessage;
use \BaseFacebook;

class facebookPush
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be FacebookPush

{
protected $facebook;
protected $errorMessage;

public function __construct(BaseFacebook $facebook) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opening bracket should be on the next line

$this->facebook = $facebook;
}
/**
* Returns error message if an executed function returned a failure (false value).
* @return string
*/
public function getErrorMessage(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opening bracket should be on the next line


return empty($this->errorMessage) ? '' : $this->errorMessage;
}

/**
* Retrieves available user information from Facebook for the logged in user. Available information
* depends on what access privileges the user has granted to your app.
* @return string|null
*/
public function getUserInfromation()
{
$access_token = self::get_access_token();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method does not exist


try {
/* Adding the locale parameter is important, because otherwise Facebook api might localize some
* variable values, such as gender.
*/
$me = json_decode(file_get_contents("https://graph.facebook.com/me?access_token={$access_token}&locale=en_US"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file_get_contents and json_decode will never throw any exception.

You code do what you expect only because of the dev ErrorHandler turning warnings into exceptions. But it means it will be broken in prod.

}
catch (\Exception $e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be } catch {

{
$this->errorMessage = $e->getMessage();

return null;
}

return $me;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not returning directly previously ? there is no need to use the $em variable

}
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing empty line

* Retrieves the profile picture of the logged in user in binary format, so that you can
* save it locally.
* @return string|null
*/
public static function getProfilePicture()
{
$facebookUID = $this->facebook->getUser();

if (empty($facebookUID)) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the new line

return null;
}

try {
$binary_image = file_get_contents("http://graph.facebook.com/{$facebookUID}/picture?type=large");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issue here about the exception

return $binary_image;
}
catch (\Exception $e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be } catch {

{
$this->errorMessage = $e->getMessage();

return null;
}
}
/**
* Publishes a message to user's Facebook stream as the user. You application name will be displayed at the bottom
* of the message.
* REQUIRES Facebook access permission "publish_stream"
* @param Rohea\FacebookBundle\Models\streamPost $streamPost Stream post object
* @param string $accessToken Optional Facebook Access token, if not given, logged in user is used
* @return boolean
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this emptu line

public function publishStream(BaseMessage $streamPost, $accessToken = null)
{
if (empty($accessToken)) {
$accessToken = $this->facebook->getAccessToken();
}
if (empty($accessToken)) {
throw new \Exception('No facebook access token, cannot post to stream');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't throw Exception directly but a SPL exception (InvalidArgumentException is probably the good choice here)

}

$streamPost->setAccessToken($accessToken);
$message = $streamPost->formatData();

try
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be try {

$result = $this->facebook->api( '/me/feed/', 'post', $message );
}
catch (\Exception $e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be } catch {

{
$this->errorMessage = $e->getMessage();

return false;
}

return true;
}
/**
* Publishes a message to user's Facebook page as the page.
* REQUIRES Facebook access permission "manage_pages"
* @param Rohea\FacebookBundle\Models\streamPost $streamPost Stream post object
* @param string $pageID Your page facebook id. You can see this for examnple in your browser uri-bar when browsing the page.
* @return boolea
*/
public function publishPage(BaseMessage $streamPost, $pageID)
{
try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be try {

{
$accessToken = $this->getPageAccessToken($pageID);
$streamPost->setAccessToken($accessToken);
$message = $streamPost->formatData();
$result = $this->facebook->api( "/{$pageID}/feed/", 'post', $message );
}
catch (\Exception $e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be } catch {

{
$this->errorMessage = $e->getMessage();

return true;
}

return true;
}
/**
* Attempts to query access token for give Facebook page
* REQUIRES Facebook access permission "manage_pages"
* @param string $pageID Facebook page id
* @throws \Exception
* @return string
*/
public function getPageAccessToken($pageID)
{
$page_info = $this->facebook->api("/$pageID?fields=access_token");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use camel-cased names


if( empty($page_info['access_token']) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be if (

throw new \Exception("Could not retrieve access token for the page $pageID");
}

return $page_info['access_token'];
}
}