| 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 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var ProvidersContainerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private $providersContainer; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(ProvidersContainerInterface $providersContainer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->providersContainer = $providersContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->request = $providersContainer->getRequest(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->response = $providersContainer->getResponse(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * Login and parsing csrfToken from cookies if success | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $username | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $password | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function login($username, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->_checkCredentials($username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $res = $this->pinners->login($username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $res; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $provider | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return Provider | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function __get($provider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $provider = strtolower($provider); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return $this->providersContainer->getProvider($provider); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * Validated password and login | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param string $username | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param string $password | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected function _checkCredentials($username, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (!$username || !$password) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             throw new LogicException('You must set username and password to login.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getLastError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->providersContainer->getResponse()->getLastError(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  | } | 
            
                        
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: