/** * Handle postTo* methods. * * @param string $method * @param array $arguments */ function __call($method, $arguments) { if (text($method)->startsWith('get')) { // a get*($parameters) method? if (empty($this->id)) { throw new \Exception('Can\'t fetch a connection without an id'); } if (count($arguments) > 0) { $parameters = $arguments[0]; } else { $parameters = array(); } $connection = lcfirst(substr($method, 3)); $connections = $this->getKnownConnections(array('id' => $this->id)); if (isset($connections[$connection]['class'])) { $class = $connections[$connection]['class']; } else { $class = '\\Sledgehammer\\GraphObject'; } if (isset($connections[$connection]['permission']) && $connections[$connection]['permission'] !== 'denied' && in_array($connections[$connection]['permission'], Facebook::getInstance()->getPermissions()) === false) { notice('Connection "' . $connection . '" requires the "' . $connections[$connection]['permission'] . '" permission', 'Current permissions: ' . quoted_human_implode(' and ', Facebook::getInstance()->getPermissions())); } $objects = array(); $response = Facebook::all($this->id . '/' . $connection, $parameters); foreach ($response as $data) { $objects[] = new $class($data); } if (empty($arguments['fields'])) { foreach ($objects as $object) { $object->_state = 'partial'; } } return new Collection($objects); } if (text($method)->startsWith('postTo')) { // a postTo*($data) method? if (empty($this->id)) { throw new \Exception('Can\'t post to a connection without an id'); } if (count($arguments) > 0) { $parameters = $arguments[0]; } else { notice('Missing argument 1 for ' . $method . '()'); $parameters = array(); } $response = Facebook::post($this->id . '/' . lcfirst(substr($method, 6)), $parameters); return new GraphObject($response['id']); } else { return parent::__call($method, $arguments); } }
protected function postToFacebook($confession, $user = null) { if (env('MANUAL_MODE', false)) { return 0; } if ($confession->images) { if ($confession->fb_post_id) { $endpoint = '/' . $confession->fb_post_id; } else { $endpoint = '/' . env('FACEBOOK_PAGE_ID', '') . '/photos'; } $response = \Facebook::post($endpoint, ['message' => $confession->getFacebookMessage(), 'url' => $confession->images], $this->getPageToken($user))->getGraphObject(); if ($confession->fb_post_id) { return $confession->fb_post_id; } else { return $response['id']; } } else { if ($confession->fb_post_id) { $endpoint = '/' . env('FACEBOOK_PAGE_ID', '') . '_' . $confession->fb_post_id; } else { $endpoint = '/' . env('FACEBOOK_PAGE_ID', '') . '/feed'; } $response = \Facebook::post($endpoint, ['message' => $confession->getFacebookMessage()], $this->getPageToken($user))->getGraphObject(); if ($confession->fb_post_id) { return $confession->fb_post_id; } else { return explode('_', $response['id'])[1]; } } }