| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DansMaCulotte\MondialRelay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DansMaCulotte\MondialRelay\Exceptions\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Zend\Soap\Client as SoapClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class MondialRelay | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** @var */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public $soapClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public $soapOptions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         'soap_version' => SOAP_1_2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $credentials = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Construct method to build soap client and options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param array $credentials Contains site_id and and site_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param string $url Url to use for SOAP client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param array $options Options to use for SOAP client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(array $credentials, string $url = null, array $options = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         if (isset($credentials['site_id']) == false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             throw Exception::invalidCredentials("site id"); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         if (isset($credentials['site_key']) == false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             throw Exception::invalidCredentials("site key"); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->credentials = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             'Enseigne' =>  $credentials['site_id'], | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             'Key' =>  $credentials['site_key'], | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->soapClient = new SoapClient( | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $url, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             array_merge($this->soapOptions, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private function getSecurityKey(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $security = $this->credentials['Enseigne']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($params as $param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $security.= $param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $security.= $this->credentials['Key']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return strtoupper(md5($security)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Proxy method to automaticaly inject credentials and options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param string $method Method to with SOAP web services | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param array $params Parameters to send with method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function soapExec(string $method, array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $params['Security'] = $this->getSecurityKey($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $params['Enseigne'] = $this->credentials['Enseigne']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $result = $this->soapClient->$method($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
When comparing two booleans, it is generally considered safer to use the strict comparison operator.