1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Pins; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Boards; |
8
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Pinners; |
9
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
10
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Interests; |
11
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Conversations; |
12
|
|
|
use seregazhuk\PinterestBot\Contracts\ProvidersContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Bot |
16
|
|
|
* |
17
|
|
|
* @package Pinterest |
18
|
|
|
* @property string $username |
19
|
|
|
* @property string $password |
20
|
|
|
* @property Pinners $pinners |
21
|
|
|
* @property Pins $pins |
22
|
|
|
* @property Boards $boards |
23
|
|
|
* @property Interests $interests |
24
|
|
|
* @property Conversations $conversations |
25
|
|
|
*/ |
26
|
|
|
class Bot |
27
|
|
|
{ |
28
|
|
|
protected $username; |
29
|
|
|
protected $password; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ProvidersContainerInterface |
33
|
|
|
*/ |
34
|
|
|
private $providersContainer; |
35
|
|
|
|
36
|
|
|
public function __construct(ProvidersContainerInterface $providersContainer) |
37
|
|
|
{ |
38
|
|
|
$this->providersContainer = $providersContainer; |
39
|
|
|
|
40
|
|
|
$this->request = $providersContainer->getRequest(); |
|
|
|
|
41
|
|
|
$this->response = $providersContainer->getResponse(); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Login and parsing csrfToken from cookies if success |
46
|
|
|
* @param $username |
47
|
|
|
* @param $password |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function login($username, $password) |
51
|
|
|
{ |
52
|
|
|
$this->username = $username; |
53
|
|
|
$this->password = $password; |
54
|
|
|
|
55
|
|
|
$this->_checkCredentials(); |
56
|
|
|
$res = $this->pinners->login($this->username, $this->password); |
57
|
|
|
|
58
|
|
|
return $res; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $provider |
63
|
|
|
* @return Provider |
64
|
|
|
*/ |
65
|
|
|
public function __get($provider) |
66
|
|
|
{ |
67
|
|
|
$provider = strtolower($provider); |
68
|
|
|
|
69
|
|
|
return $this->providersContainer->getProvider($provider); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws LogicException |
74
|
|
|
*/ |
75
|
|
|
protected function _checkCredentials() |
76
|
|
|
{ |
77
|
|
|
if ( ! $this->username || ! $this->password) { |
78
|
|
|
throw new LogicException('You must set username and password to login.'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getLastError() |
86
|
|
|
{ |
87
|
|
|
return $this->providersContainer->getResponse()->getLastError(); |
88
|
|
|
} |
89
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: