-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marvin Oßwald
committed
Jul 19, 2016
1 parent
990cf4b
commit 510cdd0
Showing
10 changed files
with
290 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
namespace marvinosswald\Socialmedia\Drivers\Pinterest; | ||
|
||
use Abraham\TwitterOAuth\TwitterOAuth; | ||
use DirkGroenen\Pinterest\Pinterest; | ||
use marvinosswald\Socialmedia\Contracts\DriverInterface; | ||
|
||
/** | ||
* Class PinterestDriver | ||
* @package Marvinosswald\Socialmedia\Drivers\Pinterest | ||
*/ | ||
class Driver implements DriverInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $appId = ''; | ||
/** | ||
* @var string | ||
*/ | ||
public $appSecret = ''; | ||
/** | ||
* @var string | ||
*/ | ||
public $accessToken = ''; | ||
/** | ||
* @var | ||
*/ | ||
public $pinterest; | ||
/** | ||
* @var string | ||
*/ | ||
public $defaultBoard = ''; | ||
|
||
/** | ||
* Driver constructor. | ||
* @param bool $appId | ||
* @param bool $appSecret | ||
*/ | ||
public function __construct($appId = false, $appSecret = false, $accessToken = false, $defaultBoard = '') | ||
{ | ||
$this->appId = $appId ?: env('PINTEREST_APP_ID'); | ||
$this->appSecret = $appSecret ?: env('PINTEREST_APP_SECRET'); | ||
$this->accessToken = $accessToken ?: env('PINTEREST_ACCESS_TOKEN'); | ||
$this->defaultBoard = $defaultBoard ?: env('PINTEREST_DEFAULT_BOARD'); | ||
|
||
$this->pinterest = new Pinterest($this->appId,$this->appSecret); | ||
$this->pinterest->auth->setOAuthToken($this->accessToken); | ||
} | ||
|
||
/** | ||
* @param $params | ||
* @return string | ||
*/ | ||
public function post($params) | ||
{ | ||
$post = new Post($this->pinterest,$params); | ||
return $post->exec(); | ||
} | ||
|
||
public function delete($id) | ||
{ | ||
$post = Post::withId($this->pinterest,$id); | ||
return $post->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
namespace marvinosswald\Socialmedia\Drivers\Pinterest; | ||
|
||
use Abraham\TwitterOAuth\TwitterOAuth; | ||
use DirkGroenen\Pinterest\Pinterest; | ||
use marvinosswald\Socialmedia\Contracts\PostInterface; | ||
|
||
/** | ||
* Class Post | ||
* @package Marvinosswald\Socialmedia\Drivers\Pinterest | ||
*/ | ||
class Post implements PostInterface{ | ||
/** | ||
* @var string|int | ||
*/ | ||
public $id; | ||
/** | ||
* The main body of the post, otherwise called the status message. Either link, place, or message must be supplied. | ||
* | ||
* @var string | ||
*/ | ||
public $message = ''; | ||
|
||
/** | ||
* The URL of a link to attach to the post. Either link, place, or message must be supplied. Additional fields associated with link are shown below. | ||
* | ||
* @var string | ||
*/ | ||
public $link = ''; | ||
/** | ||
* Determines the preview image associated with the link. | ||
* | ||
* @var string | ||
*/ | ||
public $picture = ''; | ||
/** | ||
* Board reference like <username>/<boardname> | ||
* | ||
* @var string | ||
*/ | ||
public $board = ''; | ||
/** | ||
* @var Pinterest | ||
*/ | ||
protected $pinterest; | ||
|
||
/** | ||
* Post constructor. | ||
* @param $pinterest | ||
* @param array $params | ||
*/ | ||
public function __construct($pinterest,array $params=[]) | ||
{ | ||
$this->pinterest = $pinterest; | ||
if (!empty($params)){ | ||
foreach ($params as $key => $value){ | ||
if (gettype($value) == gettype($this->{$key})){ | ||
$this->{$key} = $value; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $driver | ||
* @param $id | ||
* @param array $params | ||
* @return Post | ||
*/ | ||
public static function withId($driver,$id,array $params=[]) | ||
{ | ||
$i = new self($driver,$params); | ||
$i->id = $id; | ||
return $i; | ||
} | ||
/** | ||
* @return string | ||
*/ | ||
public function exec() | ||
{ | ||
try{ | ||
return $this->pinterest->pins->create($this->toArray()); | ||
}catch (Exception $e){ | ||
return $e->getMessage(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @return array|object|string | ||
*/ | ||
public function delete() | ||
{ | ||
if($this->id){ | ||
try{ | ||
$post = $this->pinterest->pins->delete($this->id); | ||
return $post; | ||
}catch (Exception $e){ | ||
return $e->getMessage(); | ||
} | ||
}else{ | ||
return "Post id not set"; | ||
} | ||
} | ||
public function toArray() | ||
{ | ||
return array_filter([ | ||
'note' => $this->message, | ||
'link' => $this->link, | ||
base64_decode($this->picture,true) ? 'image_base64': 'image_url' => $this->picture, | ||
'board' => $this->board ?: $this->pinterest->defaultBoard | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
use \marvinosswald\Socialmedia\Facades\Socialmedia; | ||
|
||
class PinterestTest extends Orchestra\Testbench\TestCase { | ||
protected function getPackageProviders($app) | ||
{ | ||
return ['marvinosswald\Socialmedia\Providers\SocialmediaServiceProvider']; | ||
} | ||
|
||
protected function getPackageAliases($app) | ||
{ | ||
return [ | ||
'Socialmedia' => 'marvinosswald\Socialmedia\Facades\Socialmedia' | ||
]; | ||
} | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
if (file_exists (__DIR__."/../.env")){ | ||
$dotenv = new Dotenv\Dotenv(__DIR__."/../"); | ||
$dotenv->load(); | ||
} | ||
} | ||
|
||
public function testPinterestPost() | ||
{ | ||
$params = [ | ||
'message' => 'Test Pin', | ||
'link' => 'http://google.com', | ||
'picture' => 'http://placehold.it/300/400', | ||
'board' => 'marivnosswald/reiseziele' | ||
]; | ||
$post = Socialmedia::post($params,['pinterest']); | ||
Socialmedia::delete($post['pinterest']->id,['pinterest']); | ||
} | ||
} |