|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Exceptions\InvalidRequestException; |
|
6
|
|
|
use seregazhuk\PinterestBot\Providers\Pinners; |
|
7
|
|
|
use seregazhuk\PinterestBot\Providers\Pins; |
|
8
|
|
|
use seregazhuk\PinterestBot\Providers\Boards; |
|
9
|
|
|
use seregazhuk\PinterestBot\Providers\Interests; |
|
10
|
|
|
use seregazhuk\PinterestBot\Providers\Conversations; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PinterestBot |
|
14
|
|
|
* |
|
15
|
|
|
* @package Pinterest |
|
16
|
|
|
* @property string $username |
|
17
|
|
|
* @property string $password |
|
18
|
|
|
* @property Pinners $pinners |
|
19
|
|
|
* @property Pins $pins |
|
20
|
|
|
* @property Boards $boards |
|
21
|
|
|
* @property Interests $interests |
|
22
|
|
|
* @property Conversations $conversations |
|
23
|
|
|
*/ |
|
24
|
|
|
class PinterestBot |
|
25
|
|
|
{ |
|
26
|
|
|
protected $username; |
|
27
|
|
|
protected $password; |
|
28
|
|
|
protected $loggedIn = false; |
|
29
|
|
|
|
|
30
|
|
|
const PROVIDERS_NAMESPACE = "seregazhuk\\PinterestBot\\Providers\\"; |
|
31
|
|
|
|
|
32
|
|
|
const MAX_PAGINATED_ITEMS = 100; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($username, $password) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->username = $username; |
|
37
|
|
|
$this->password = $password; |
|
38
|
|
|
|
|
39
|
|
|
$this->request = new Request(new Http()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* A array containing the cached providers |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $providers = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* A reference to the request class which travels |
|
51
|
|
|
* through the application |
|
52
|
|
|
* |
|
53
|
|
|
* @var Request |
|
54
|
|
|
*/ |
|
55
|
|
|
public $request; |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Login and parsing csrfToken from cookies if success |
|
60
|
|
|
*/ |
|
61
|
|
|
public function login() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->_check_credentials(); |
|
64
|
|
|
$res = $this->pinners->login($this->username, $this->password); |
|
65
|
|
|
return $res; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $provider |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
* @throws InvalidRequestException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __get($provider) |
|
74
|
|
|
{ |
|
75
|
|
|
$provider = strtolower($provider); |
|
76
|
|
|
|
|
77
|
|
|
// Check if an instance has already been initiated |
|
78
|
|
|
if ( ! isset($this->providers[$provider])) { |
|
79
|
|
|
$this->_addProvider($provider); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->providers[$provider]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $provider |
|
87
|
|
|
* @throws InvalidRequestException |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function _addProvider($provider) |
|
90
|
|
|
{ |
|
91
|
|
|
$class = self::PROVIDERS_NAMESPACE . ucfirst($provider); |
|
92
|
|
|
|
|
93
|
|
|
if ( ! class_exists($class)) { |
|
94
|
|
|
throw new InvalidRequestException; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Create a reflection of the called class |
|
98
|
|
|
$ref = new \ReflectionClass($class); |
|
99
|
|
|
$obj = $ref->newInstanceArgs([$this->request]); |
|
100
|
|
|
|
|
101
|
|
|
$this->providers[$provider] = $obj; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @throws \LogicException |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function _check_credentials() |
|
108
|
|
|
{ |
|
109
|
|
|
if ( ! $this->username || ! $this->password) { |
|
110
|
|
|
throw new \LogicException('You must set username and password to login.'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |