From c55cb4bfa88d1c448749cd234cbb97b79dc34b66 Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Fri, 18 Nov 2011 12:36:00 +0200 Subject: [PATCH 1/7] facebook push --- Models/streamPost.php | 101 ++++++++++++++++++++++++ Services/facebookPush.php | 158 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 Models/streamPost.php create mode 100644 Services/facebookPush.php diff --git a/Models/streamPost.php b/Models/streamPost.php new file mode 100644 index 0000000..9feaad3 --- /dev/null +++ b/Models/streamPost.php @@ -0,0 +1,101 @@ + + * + * This class functions as a model for messages to be sent by the facebookPush-service to the Facebook api. + */ + +namespace FOS\FacebookBundle\Models; + +class streamPost +{ + 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) + * @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; + } + /** + * 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( + '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; + } +} +?> \ No newline at end of file diff --git a/Services/facebookPush.php b/Services/facebookPush.php new file mode 100644 index 0000000..9d2f6ac --- /dev/null +++ b/Services/facebookPush.php @@ -0,0 +1,158 @@ + + * + * File provides a service that abstracts Facebook API functionality such as retrieving user information and + * posting data to Facebook API. + */ +namespace FOS\FacebookBundle\Services; + +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use FOS\FacebookBundle\Models\streamPost as BaseMessage; +use \BaseFacebook; + +class facebookPush +{ + protected $facebook; + protected $errorMessage; + + public function __construct(BaseFacebook $facebook) { + $this->facebook = $facebook; + } + /** + * Returns error message if an executed function returned a failure (false value). + * @return string + */ + public function getErrorMessage(){ + + 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(); + + 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")); + } + catch (\Exception $e) + { + $this->errorMessage = $e->getMessage(); + + return null; + } + + return $me; + } + /** + * 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)) { + + return null; + } + + try { + $binary_image = file_get_contents("http://graph.facebook.com/{$facebookUID}/picture?type=large"); + return $binary_image; + } + catch (\Exception $e) + { + $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 + */ + + 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'); + } + + $streamPost->setAccessToken($accessToken); + $message = $streamPost->formatData(); + + try + { + $result = $this->facebook->api( '/me/feed/', 'post', $message ); + } + catch (\Exception $e) + { + $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 + { + $accessToken = $this->getPageAccessToken($pageID); + $streamPost->setAccessToken($accessToken); + $message = $streamPost->formatData(); + $result = $this->facebook->api( "/{$pageID}/feed/", 'post', $message ); + } + catch (\Exception $e) + { + $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"); + + if( empty($page_info['access_token']) ) { + throw new \Exception("Could not retrieve access token for the page $pageID"); + } + + return $page_info['access_token']; + } +} From 6de086024921db27370b937f09a4ade2cb0eadf7 Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Fri, 18 Nov 2011 12:38:36 +0200 Subject: [PATCH 2/7] konffi --- Resources/config/facebook.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Resources/config/facebook.xml b/Resources/config/facebook.xml index 983aaf9..df7c6d4 100644 --- a/Resources/config/facebook.xml +++ b/Resources/config/facebook.xml @@ -30,6 +30,10 @@ + + + + From 9d2c9d379f750c1f2846098183476a0e94aacc15 Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Fri, 18 Nov 2011 14:43:04 +0200 Subject: [PATCH 3/7] Service name and parameters fixed --- Resources/config/facebook.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Resources/config/facebook.xml b/Resources/config/facebook.xml index df7c6d4..ca7ee76 100644 --- a/Resources/config/facebook.xml +++ b/Resources/config/facebook.xml @@ -31,8 +31,8 @@ - - + + From 668e45920d2554361004f9d24f36d0a05201118b Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Mon, 21 Nov 2011 14:04:16 +0200 Subject: [PATCH 4/7] pois --- Services/facebookPush.php | 158 -------------------------------------- 1 file changed, 158 deletions(-) delete mode 100644 Services/facebookPush.php diff --git a/Services/facebookPush.php b/Services/facebookPush.php deleted file mode 100644 index 9d2f6ac..0000000 --- a/Services/facebookPush.php +++ /dev/null @@ -1,158 +0,0 @@ - - * - * File provides a service that abstracts Facebook API functionality such as retrieving user information and - * posting data to Facebook API. - */ -namespace FOS\FacebookBundle\Services; - -use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use FOS\FacebookBundle\Models\streamPost as BaseMessage; -use \BaseFacebook; - -class facebookPush -{ - protected $facebook; - protected $errorMessage; - - public function __construct(BaseFacebook $facebook) { - $this->facebook = $facebook; - } - /** - * Returns error message if an executed function returned a failure (false value). - * @return string - */ - public function getErrorMessage(){ - - 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(); - - 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")); - } - catch (\Exception $e) - { - $this->errorMessage = $e->getMessage(); - - return null; - } - - return $me; - } - /** - * 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)) { - - return null; - } - - try { - $binary_image = file_get_contents("http://graph.facebook.com/{$facebookUID}/picture?type=large"); - return $binary_image; - } - catch (\Exception $e) - { - $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 - */ - - 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'); - } - - $streamPost->setAccessToken($accessToken); - $message = $streamPost->formatData(); - - try - { - $result = $this->facebook->api( '/me/feed/', 'post', $message ); - } - catch (\Exception $e) - { - $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 - { - $accessToken = $this->getPageAccessToken($pageID); - $streamPost->setAccessToken($accessToken); - $message = $streamPost->formatData(); - $result = $this->facebook->api( "/{$pageID}/feed/", 'post', $message ); - } - catch (\Exception $e) - { - $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"); - - if( empty($page_info['access_token']) ) { - throw new \Exception("Could not retrieve access token for the page $pageID"); - } - - return $page_info['access_token']; - } -} From 21f079273c2120e72e08d7d3964d018b26661e63 Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Mon, 21 Nov 2011 14:06:33 +0200 Subject: [PATCH 5/7] Modifications and fixes based on feedback from a pull request --- Model/StreamPost.php | 109 ++++++++++++++++++++++ Resources/config/facebook.xml | 4 +- Services/FacebookPush.php | 166 ++++++++++++++++++++++++++++++++++ 3 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 Model/StreamPost.php create mode 100644 Services/FacebookPush.php diff --git a/Model/StreamPost.php b/Model/StreamPost.php new file mode 100644 index 0000000..f3572a9 --- /dev/null +++ b/Model/StreamPost.php @@ -0,0 +1,109 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * This class functions as a model for messages to be sent by the facebookPush-service to the Facebook api. + * + * @author Teemu Reisbacka + */ +namespace FOS\FacebookBundle\Model; + +use \InvalidArgumentException; + +class StreamPost +{ + 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) + * @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; + } + } + /** + * Formats this object into on array that can be fed to the php-api class. This function will be called automatically. + * @throws \InvalidArgumentException; + * @return array + */ + public function formatData() + { + if (empty($this->accessToken)) { + throw new \InvalidArgumentException('cannot format Facebook message: Facebook access token is empty'); + } + if (empty($this->message)) { + throw new \InvalidArgumentException('cannot format Facebook message: Message is empty, nothing to send'); + } + $streamPost = array( + 'access_token' => $this->accessToken, + 'message' => $this->message + ); + if (!empty($this->linkout)) { + $streamPost = array_merge($streamPost, $this->linkout); + } + if (!empty($this->attachment)) { + $streamPost = array_merge($streamPost, $this->attachment); + } + + return $streamPost; + } +} \ No newline at end of file diff --git a/Resources/config/facebook.xml b/Resources/config/facebook.xml index ca7ee76..53adce3 100644 --- a/Resources/config/facebook.xml +++ b/Resources/config/facebook.xml @@ -30,8 +30,8 @@ - - + + diff --git a/Services/FacebookPush.php b/Services/FacebookPush.php new file mode 100644 index 0000000..c8253f1 --- /dev/null +++ b/Services/FacebookPush.php @@ -0,0 +1,166 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * This class provides a service that abstracts Facebook API functionality such as retrieving user information and + * posting data to Facebook API. + * + * @author Teemu Reisbacka + */ +namespace FOS\FacebookBundle\Services; + +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use FOS\FacebookBundle\Model\StreamPost as BaseMessage; +use \BaseFacebook; +use \FacebookApiException; +use \InvalidArgumentException; +use \UnexpectedValueException; + +class FacebookPush +{ + protected $facebook; + protected $errorMessage; + + public function __construct(BaseFacebook $facebook) + { + $this->facebook = $facebook; + } + /** + * Returns error message if an executed function returned a failure (false value). + * @return string + */ + public function getErrorMessage() + { + + 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 array|null + */ + public function getUserInfromation() + { + $accessToken = $this->facebook->getAccessToken(); + /* 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={$accessToken}&locale=en_US"), true); + + return $me; + } + /** + * Retrieves the profile picture of the logged in user in binary format, so that you can + * save it locally. + * @return string|boolean + */ + public function getProfilePicture() + { + $facebookUID = $this->facebook->getUser(); + if (empty($facebookUID)) { + + return null; + } + + $binaryImage = file_get_contents("http://graph.facebook.com/{$facebookUID}/picture?type=large"); + + return $binaryImage; + } + /** + * 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 + */ + + public function publishStream(BaseMessage $streamPost, $accessToken = null) + { + if (empty($accessToken)) { + $accessToken = $this->facebook->getAccessToken(); + } + if (empty($accessToken)) { + throw new InvalidArgumentException('No facebook access token, cannot post to stream'); + } + + $streamPost->setAccessToken($accessToken); + $message = $streamPost->formatData(); + + try { + $result = $this->facebook->api( '/me/feed/', 'post', $message ); + + /* Confirm that the post went through -> Facebook api return message id + */ + if (!is_array($result) || empty($result["id"])) { + throw new UnexpectedValueException("Did not receive message id back from the api, post failed."); + } + } + catch (FacebookApiException $e) { + $this->errorMessage = $e->getMessage(); + + return false; + } catch (UnexpectedValueException $e) { + $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 { + $accessToken = $this->getPageAccessToken($pageID); + $streamPost->setAccessToken($accessToken); + $message = $streamPost->formatData(); + $result = $this->facebook->api( "/{$pageID}/feed/", 'post', $message ); + + /* Confirm that the post went through -> Facebook api return message id + */ + if (!is_array($result) || empty($result["id"])) { + throw new UnexpectedValueException("Did not receive message id back from the api, post failed."); + } + } catch (FacebookApiException $e) { + $this->errorMessage = $e->getMessage(); + + return false; + } catch (UnexpectedValueException $e) { + $this->errorMessage = $e->getMessage(); + + return false; + } + + return true; + } + /** + * Attempts to query access token for give Facebook page + * REQUIRES Facebook access permission "manage_pages" + * @param string $pageID Facebook page id + * @throws \UnexpectedValueException + * @return string + */ + public function getPageAccessToken($pageID) + { + $pageInfo = $this->facebook->api("/$pageID?fields=access_token"); + + if (empty($pageInfo['access_token'])) { + throw new UnexpectedValueException("Could not retrieve access token for the page $pageID"); + } + + return $pageInfo['access_token']; + } +} \ No newline at end of file From 6ba8f8b6fd14efb8a40ab8bf3e494a958d6ef4b8 Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Mon, 21 Nov 2011 14:29:12 +0200 Subject: [PATCH 6/7] Removing models folder --- Models/streamPost.php | 101 ------------------------------------------ 1 file changed, 101 deletions(-) delete mode 100644 Models/streamPost.php diff --git a/Models/streamPost.php b/Models/streamPost.php deleted file mode 100644 index 9feaad3..0000000 --- a/Models/streamPost.php +++ /dev/null @@ -1,101 +0,0 @@ - - * - * This class functions as a model for messages to be sent by the facebookPush-service to the Facebook api. - */ - -namespace FOS\FacebookBundle\Models; - -class streamPost -{ - 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) - * @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; - } - /** - * 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( - '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; - } -} -?> \ No newline at end of file From dc771c9699f4cbbc54d8f0289ca0ddab9f9c5644 Mon Sep 17 00:00:00 2001 From: Teemu Reisbacka Date: Mon, 5 Dec 2011 15:43:22 +0200 Subject: [PATCH 7/7] Adjusted formatting of lines, comments and license. Adjusted thrown exceptions. --- Model/StreamPost.php | 27 ++++++++++++++++------ Services/FacebookPush.php | 47 ++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/Model/StreamPost.php b/Model/StreamPost.php index f3572a9..fd3dce9 100644 --- a/Model/StreamPost.php +++ b/Model/StreamPost.php @@ -1,4 +1,5 @@ */ + namespace FOS\FacebookBundle\Model; use \InvalidArgumentException; +/** + * + * This class functions as a model for messages to be sent by the facebookPush-service to the Facebook api. + * @author Teemu Reisbacka + * + */ class StreamPost { protected $accessToken; protected $message; protected $attachment; + protected $linkout; /** * 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) @@ -41,6 +48,8 @@ public function setMessage($message) /** * Adds an attachment-picture to your Facebook message. If you specify a link, the image will function as a link. + * If you set an attachment, you cannot set any additional links with setLink(...). + * * @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. @@ -63,9 +72,11 @@ public function setAttachment($name, $caption, $uriAttachment, $uriLinkOut = nul } $this->attachment = $attachment; } + /** - * Shortcut for adding a link to you Facebook-message without an image. - * The setAttachment(...) will override this function. + * Shortcut for adding a link to you Facebook-message without an image. If you + * specify an attachment with setAttachment(...), that will be used instead of this link. + * * @param string $name This is the title of the link (top-most descriptive text) * @param string $linkOut Full uri to where you wish to link to * @param string $caption Optional description of the link @@ -80,8 +91,10 @@ public function setLink($name, $linkOut, $caption = null) $this->linkout['caption'] = $caption; } } + /** * Formats this object into on array that can be fed to the php-api class. This function will be called automatically. + * * @throws \InvalidArgumentException; * @return array */ diff --git a/Services/FacebookPush.php b/Services/FacebookPush.php index c8253f1..b7a2aa1 100644 --- a/Services/FacebookPush.php +++ b/Services/FacebookPush.php @@ -1,4 +1,5 @@ */ + namespace FOS\FacebookBundle\Services; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -21,6 +18,13 @@ use \InvalidArgumentException; use \UnexpectedValueException; +/** + * + * This class provides a service that abstracts Facebook API functionality such as retrieving user information and + * posting data to Facebook API. + * @author Teemu Reisbacka + * + */ class FacebookPush { protected $facebook; @@ -30,8 +34,10 @@ public function __construct(BaseFacebook $facebook) { $this->facebook = $facebook; } + /** * Returns error message if an executed function returned a failure (false value). + * * @return string */ public function getErrorMessage() @@ -43,6 +49,7 @@ public function getErrorMessage() /** * 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 array|null */ public function getUserInfromation() @@ -55,16 +62,17 @@ public function getUserInfromation() return $me; } + /** * Retrieves the profile picture of the logged in user in binary format, so that you can * save it locally. + * * @return string|boolean */ public function getProfilePicture() { $facebookUID = $this->facebook->getUser(); if (empty($facebookUID)) { - return null; } @@ -72,15 +80,16 @@ public function getProfilePicture() return $binaryImage; } + /** * 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 */ - public function publishStream(BaseMessage $streamPost, $accessToken = null) { if (empty($accessToken)) { @@ -92,21 +101,16 @@ public function publishStream(BaseMessage $streamPost, $accessToken = null) $streamPost->setAccessToken($accessToken); $message = $streamPost->formatData(); - try { $result = $this->facebook->api( '/me/feed/', 'post', $message ); /* Confirm that the post went through -> Facebook api return message id */ if (!is_array($result) || empty($result["id"])) { - throw new UnexpectedValueException("Did not receive message id back from the api, post failed."); + $this->errorMessage = "Did not receive message id back from the api, post failed."; + return false; } - } - catch (FacebookApiException $e) { - $this->errorMessage = $e->getMessage(); - - return false; - } catch (UnexpectedValueException $e) { + } catch (FacebookApiException $e) { $this->errorMessage = $e->getMessage(); return false; @@ -114,12 +118,14 @@ public function publishStream(BaseMessage $streamPost, $accessToken = null) 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 + * @return boolean */ public function publishPage(BaseMessage $streamPost, $pageID) { @@ -132,23 +138,22 @@ public function publishPage(BaseMessage $streamPost, $pageID) /* Confirm that the post went through -> Facebook api return message id */ if (!is_array($result) || empty($result["id"])) { - throw new UnexpectedValueException("Did not receive message id back from the api, post failed."); + $this->errorMessage = "Did not receive message id back from the api, post failed."; + return false; } } catch (FacebookApiException $e) { $this->errorMessage = $e->getMessage(); - return false; - } catch (UnexpectedValueException $e) { - $this->errorMessage = $e->getMessage(); - return false; } return true; } + /** * Attempts to query access token for give Facebook page * REQUIRES Facebook access permission "manage_pages" + * * @param string $pageID Facebook page id * @throws \UnexpectedValueException * @return string