|
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\ProvidersContainer; |
|
9
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
|
10
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
|
11
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Pins; |
|
12
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Boards; |
|
13
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Pinners; |
|
14
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
|
15
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Interests; |
|
16
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Conversations; |
|
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
|
|
|
* @var ProvidersContainer |
|
37
|
|
|
*/ |
|
38
|
|
|
private $providersContainer; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* References to the request and response classes that travels |
|
42
|
|
|
* through the application |
|
43
|
|
|
* |
|
44
|
|
|
* @var Request |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $request; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var Response |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $response; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct($username, $password) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->username = $username; |
|
55
|
|
|
$this->password = $password; |
|
56
|
|
|
|
|
57
|
|
|
$this->request = new Request(new CurlAdaptor()); |
|
58
|
|
|
$this->response = new Response(); |
|
59
|
|
|
|
|
60
|
|
|
$this->providersContainer = new ProvidersContainer($this->request, $this->response); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Login and parsing csrfToken from cookies if success |
|
65
|
|
|
*/ |
|
66
|
|
|
public function login() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->_checkCredentials(); |
|
69
|
|
|
$res = $this->pinners->login($this->username, $this->password); |
|
70
|
|
|
|
|
71
|
|
|
return $res; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $provider |
|
76
|
|
|
* @return Provider |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __get($provider) |
|
79
|
|
|
{ |
|
80
|
|
|
$provider = strtolower($provider); |
|
81
|
|
|
|
|
82
|
|
|
return $this->providersContainer->getProvider($provider); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws LogicException |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function _checkCredentials() |
|
89
|
|
|
{ |
|
90
|
|
|
if ( ! $this->username || ! $this->password) { |
|
91
|
|
|
throw new LogicException('You must set username and password to login.'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getLastError() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->response->getLastError(); |
|
101
|
|
|
} |
|
102
|
|
|
} |