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