| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://flipboxfactory.com/software/hubspot/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://www.flipboxfactory.com/software/hubspot/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace flipbox\craft\hubspot\fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use craft\helpers\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use flipbox\craft\hubspot\criteria\CompanyCriteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use flipbox\craft\hubspot\HubSpot; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Companies extends Objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function getObjectLabel(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return 'Company'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public static function displayName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return HubSpot::t('HubSpot: Companies'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public static function defaultSelectionLabel(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return HubSpot::t('Add a HubSpot Company'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws \flipbox\craft\integration\exceptions\ConnectionNotFound | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     protected function upsertToHubSpot( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         array $payload, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         string $id = null | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     ): ResponseInterface { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         return (new CompanyCriteria([ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             'connection' => $this->getConnection(), | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'cache' => $this->getCache(), | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             'payload' => $payload, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             'id' => $id | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         ]))->upsert(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws \flipbox\craft\integration\exceptions\ConnectionNotFound | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function readFromHubSpot( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     ): ResponseInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return (new CompanyCriteria([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'connection' => $this->getConnection(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'cache' => $this->getCache(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'id' => $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]))->read(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param ResponseInterface $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     protected function getObjectIdFromResponse(ResponseInterface $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $data = Json::decodeIfJson( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $response->getBody()->getContents() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $id = $data['companyId'] ?? ($data['vid'] ?? null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $id ? (string)$id : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |