├── public
└── .gitkeep
├── src
├── lang
│ └── .gitkeep
├── views
│ └── .gitkeep
├── config
│ ├── .gitkeep
│ └── config.php
├── migrations
│ └── .gitkeep
├── controllers
│ └── .gitkeep
└── Tinyrocket
│ └── Magento
│ ├── Objects
│ ├── Exceptions.php
│ ├── MagentoObjectCollection.php
│ └── MagentoObject.php
│ ├── Facades
│ ├── Magento.php
│ ├── MagentoSoapClient.php
│ └── MagentoSoapStorage.php
│ ├── Connections
│ ├── Exceptions.php
│ ├── MagentoSoapStorage.php
│ └── MagentoSoapClient.php
│ ├── MagentoServiceProvider.php
│ └── Magento.php
├── tests
└── .gitkeep
├── .gitignore
├── .travis.yml
├── phpunit.xml
├── composer.json
└── README.md
/public/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/lang/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/views/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/config/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/migrations/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/controllers/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | composer.phar
3 | composer.lock
4 | .DS_Store
5 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 5.4
5 | - 5.5
6 | - 5.6
7 | - hhvm
8 |
9 | before_script:
10 | - travis_retry composer self-update
11 | - travis_retry composer install --prefer-source --no-interaction --dev
12 |
13 | script: phpunit
14 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
'; 114 | print_r($functions); 115 | echo ''; 116 | return; 117 | } 118 | } 119 | 120 | /** 121 | * Camelize 122 | * 123 | * @return string 124 | */ 125 | public function camelize($key) 126 | { 127 | return 'get' . implode('', array_map('ucfirst', array_map('strtolower', explode('_', $key)))); 128 | } 129 | 130 | /** 131 | * Set/Get attribute wrapper 132 | * 133 | * @see Varien_Object 134 | * @return mixed 135 | */ 136 | public function __call($method, $args) 137 | { 138 | if (substr($method, 0, 3) == 'get') { 139 | $key = $this->underscore(substr($method,3)); 140 | $data = $this->getData($key, isset($args[0]) ? $args[0] : null); 141 | return $data; 142 | } 143 | } 144 | 145 | /** 146 | * Converts field names for setters and geters 147 | * 148 | * @see Varien_Object 149 | * @return string 150 | */ 151 | protected function underscore($name) 152 | { 153 | return strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name)); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Connections/MagentoSoapClient.php: -------------------------------------------------------------------------------- 1 | 38 | * @copyright 2014 TinyRocket 39 | * 40 | */ 41 | class MagentoSoapClient extends \SoapClient { 42 | 43 | /** 44 | * @var wsdl 45 | */ 46 | protected $wsdl; 47 | 48 | /** 49 | * @var connection 50 | */ 51 | protected $connection; 52 | 53 | /** 54 | * @var client 55 | */ 56 | protected $session; 57 | 58 | /** 59 | * @var results 60 | */ 61 | protected $results; 62 | 63 | /** 64 | * @var debugTrace 65 | */ 66 | protected $debugTrace; 67 | 68 | /** 69 | * Constructor 70 | * 71 | * @return void 72 | */ 73 | public function __construct($connection = null, $options = array('trace' => 1)) 74 | { 75 | if ( !is_null($connection) ) { 76 | try { 77 | $this->connection = $connection[key($connection)]; 78 | $this->wsdl = $this->getConstructedUrl(); 79 | 80 | parent::__construct($this->wsdl, $options); 81 | 82 | $this->session = $this->login($this->connection['user'], $this->connection['key']); 83 | } catch (Exception $e) { 84 | throw new MagentoSoapClientException($e->getMessage()); 85 | } 86 | 87 | } 88 | 89 | } 90 | 91 | /** 92 | * Capture and format SOAP calls for both 93 | * v1 and v2 of the Magento api. 94 | */ 95 | public function __call($method, $args) 96 | { 97 | if ( is_array($args) && count($args) == 1 ) { 98 | $args = current($args); 99 | } 100 | 101 | try { 102 | if ( $method == 'login' ) { 103 | $this->results = $this->__soapCall($method, $args); 104 | } elseif ($method == 'call') { 105 | $this->results = $this->__soapCall($method, $args); 106 | } else { 107 | $this->results = $this->__soapCall($method, array($this->session, $args)); 108 | } 109 | 110 | return $this->getResultsCollections(); 111 | } catch ( \Exception $e) { 112 | throw new MagentoSoapClientException($e->getMessage()); 113 | } 114 | } 115 | 116 | public function call($method, $params) 117 | { 118 | try { 119 | $data = array_merge(array($this->session, $method), array($params)); 120 | return parent::call($data); 121 | } catch (Exception $e) { 122 | throw new MagentoSoapClientException($e->getMessage()); 123 | } 124 | } 125 | 126 | /** 127 | * Get Results Collection 128 | * 129 | * Determines whether to return a formatted collection for 130 | * array returns, a single object for single array returns 131 | * or simply the response, for single var/int returns 132 | * 133 | * @return mixed 134 | */ 135 | public function getResultsCollections() 136 | { 137 | if ( is_array($this->results) && $this->isMulti() ) { 138 | 139 | return new MagentoObjectCollection($this->results); 140 | 141 | } elseif( is_array($this->results) && !$this->isMulti() ) { 142 | 143 | return new MagentoObject($this->results); 144 | 145 | } elseif( is_object($this->results) ) { 146 | 147 | return new MagentoObject($this->results); 148 | 149 | } else { 150 | 151 | return $this->results; 152 | } 153 | } 154 | 155 | /** 156 | * Get Functions 157 | * 158 | * Extension of the __getFunctions method core to SoapClient 159 | * 160 | * @return array 161 | */ 162 | public function getFunctions() 163 | { 164 | return $this->__getFunctions(); 165 | } 166 | 167 | /** 168 | * Get Last Response 169 | * 170 | * Extension of the __getLastResponse method core to SoapClient 171 | * 172 | * @return array 173 | */ 174 | public function getLastResponse() 175 | { 176 | return $this->__getLastResponse(); 177 | } 178 | 179 | /** 180 | * Get Soap Version 181 | * 182 | * Returns the selected version of the SOAP API 183 | * 184 | * @return string 185 | */ 186 | public function getSoapVersion() 187 | { 188 | 189 | $version = isset($this->connection['version']) ? $this->connection['version'] : ''; 190 | 191 | if ( isset($this->connection['version']) && in_array(strtolower($this->connection['version']), $this->getAvailableVersions()) ) { 192 | 193 | // Return the requested version 194 | return strtolower($this->connection['version']); 195 | 196 | } elseif ( !isset($this->connection['version']) || !strlen($this->connection['version']) ) { 197 | 198 | // Fallback to v2 if no version is supplied 199 | return end($this->getAvailableVersions()); 200 | } 201 | throw new MagentoSoapConfigurationException("The supplied version [$version] is invalid. Please check your configuration."); 202 | } 203 | 204 | /** 205 | * Get Applicable Soap Version 206 | * 207 | * Returns an array of possible API versoins 208 | * 209 | * @return string 210 | * @todo add support for XML responses 211 | */ 212 | public function getAvailableVersions() 213 | { 214 | return array('v1', 'v2'); 215 | } 216 | 217 | /** 218 | * Is Multi 219 | * 220 | * Check whether the returned result is a multi-level array 221 | * 222 | * @return bool 223 | */ 224 | protected function isMulti() 225 | { 226 | return isset($this->results[0]); 227 | } 228 | 229 | /** 230 | * Test SOAP Connection 231 | * 232 | * Returns either a boolean reponse or response headers, depending 233 | * on whether $showHeaders is set to true. 234 | * 235 | * @return mixed 236 | */ 237 | public function testConnection($connection, $showHeaders) 238 | { 239 | $testUrl = $this->getConstructedUrl($connection); 240 | try { 241 | file_get_contents($testUrl); 242 | if ( $showHeaders === true ) { 243 | return $http_response_header; 244 | } 245 | $responseCode = array_values($http_response_header)[0]; 246 | return strpos($responseCode, '200 OK') === false ? false : true; 247 | 248 | // No Response returned 249 | } catch ( \Exception $e ) { 250 | if ( $showHeaders === true ) { 251 | return "No response headers. Test failed."; 252 | } 253 | 254 | return false; 255 | } 256 | } 257 | 258 | /** 259 | * Get Magento Version 260 | * 261 | * Returns the Magento build version for either the default 262 | * connection or the connection passed through to the function 263 | * 264 | * @return string 265 | * @example 266 | */ 267 | public function getMagentoVersion() 268 | { 269 | $version = $this->getSoapVersion(); 270 | switch ($version) { 271 | case 'v1': 272 | $response = $this->call('core_magento.info', array()); 273 | break; 274 | 275 | case 'v2': 276 | $response = $this->__call('magentoInfo', array()); 277 | break; 278 | } 279 | 280 | if ( isset($response) ) { 281 | return sprintf('%s %s', $response->getMagentoEdition(), $response->getMagentoVersion()); 282 | } 283 | } 284 | 285 | /** 286 | * Construct URL 287 | * 288 | * Used to created either a Soap V1 or V2 URL for the WSDL 289 | * based on the configuration for the primary connection. 290 | * 291 | * @return string 292 | * @todo modify to work with rewrites 293 | */ 294 | private function getConstructedUrl($connection = null) 295 | { 296 | $url = is_null($connection) ? rtrim($this->connection['site_url'], '/') : rtrim($connection[key($connection)]['site_url'], '/'); 297 | $version = is_null($connection) ? strtolower($this->connection['version']) : strtolower($connection[key($connection)]['version']); 298 | 299 | switch ($version) { 300 | case 'v1': 301 | return sprintf("%s/api/soap/?wsdl", $url); 302 | break; 303 | 304 | case 'v2': 305 | return sprintf("%s/api/v2_soap?wsdl", $url); 306 | break; 307 | 308 | default: 309 | return sprintf("%s/api/v2_soap?wsdl", $url); 310 | break; 311 | } 312 | } 313 | 314 | public function __destruct() 315 | { 316 | 317 | } 318 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Magento SOAP Integration for Laravel 2 | =========================== 3 | 4 | A simple and somewhat intuitive package for managing and interacting with the Magento SOAP Api. Compatible with Laravel 4 and Magneto SOAP v1 & v2. 5 | 6 | [](https://travis-ci.org/TinyRocket/laravel-magento-integration) [](https://packagist.org/packages/tinyrocket/magento) [](https://packagist.org/packages/tinyrocket/magento) [](https://packagist.org/packages/tinyrocket/magento) [](https://packagist.org/packages/tinyrocket/magento) 7 | 8 | > **Note:** This package is still in the beta phase of development. I'd advise against using to test against a production Magento application. This package is for Laravel 4, if you're looking for a Laravel 3 version of this package, checkout [MatteoCastiglioni's bundle](https://github.com/MatteoCastiglioni/magentoapi) 9 | 10 | 11 | ### Installation 12 | 13 | To install via composer, add the following to your requirements 14 | 15 | "require": { 16 | ... 17 | "tinyrocket/magento": "1.0.*" 18 | ... 19 | }, 20 | **Note:** You may need to change your **minimum-stability** to **dev** 21 | 22 | ### Configuration 23 | 24 | Add the following to your Laravel Application configuration (app/config/app.php) 25 | 26 | To your **providers** array 27 | 28 | 29 | 'providers' => array( 30 | ... 31 | 'Tinyrocket\Magento\MagentoServiceProvider', 32 | ... 33 | ), 34 | 35 | and to your **aliases** array 36 | 37 | 38 | 'aliases' => array( 39 | ... 40 | 'Magento' => 'Tinyrocket\Magento\Facades\Magento', 41 | ... 42 | ), 43 | 44 | Publish the the package configuration file by running the following in CLI 45 | 46 | php artisan config:publish tinyrocket/magento 47 | 48 | #### Setting up the SOAP connections 49 | 50 | The quickest way to get started with Magento integration is to add your connection(s) to the newly published configuration file. The file can be found in 51 | 52 | app/config/packages/tinyrocket/magento/config.php 53 | 54 | There is no limit to the amount of connections that you can save, but you should set a default configuration to handle fallbacks. Inside of the configuration file, set the following with your Magento SOAP information. 55 | 56 | **Note:** Use your store's base URL, not the URL for the SOAP endpoint. (e.g. http://magentostore.com rather than http://magentostore.com/api/soap?wsdl) 57 | 58 | 'connections' => [ 59 | ... 60 | 'default' => [ 61 | 'site_url' => 'http://magentohost', 62 | 'user' => '', 63 | 'key' => '', 64 | 'version' => 'v2' 65 | ], 66 | ... 67 | ] 68 | 69 | The first parameter defines the name of the connection and should be unique. The API version is optional and will default to v2 for all connections, unless set otherwise. 70 | 71 | ##Usage 72 | 73 | An exhaustive list of possible methods is available on the [Magento Website](http://www.magentocommerce.com/api/soap/) 74 | 75 | There are two basic methods used to interact with the SOAP Api 76 | 77 | For **SOAP v2** 78 | 79 | Magento::call()->someSoapRequest($requestParams) 80 | 81 | // Example 82 | Magento::call()->customerCustomerList() 83 | 84 | For **SOAP v1** 85 | 86 | Magento::get('some.method', array($requestParams)) 87 | 88 | //Example 89 | Magento::get('customer.list') 90 | 91 | Alternatively, you can call methods directly for **SOAP v2** requests 92 | 93 | Magento::someSoapRequest($requestParams) 94 | 95 | // Example 96 | $customerParams = array( 97 | 'email' => 'customer@example.org', 98 | 'firstname' => 'Dough', 99 | 'lastname' => 'Deeks', 100 | 'password' => 'password', 101 | 'website_id' => 1, 102 | 'store_id' => 1, 103 | 'group_id' => 1 104 | ); 105 | $customerId = Magento::customerCustomerCreate($customerParams) 106 | 107 | 108 | ###Working with the results 109 | 110 | To make working with the SOAP API slightly more intuitive, some request results are returned as data objects and collections, inspired by **Varien_Object** and **Varien_Object_Collection** classes in Magento. These classes allow for the calling of information with some basic methods. 111 | 112 | ####Objects Collections 113 | For SOAP responses that return a group of items, results are returned as object collections containing individual objects. These collections have four basic methods. 114 | 115 | **getCollection()** - Returns all items as a group of objects 116 | 117 | **getCount()** - Returns number of items in the collection 118 | 119 | **getFirstItem()** - Returns the first response item 120 | 121 | **getLastItem()** - Returns the last response item 122 | 123 | foreach ( Magento::salesOrderList()->getCollection() as $order ) { 124 | // Do stuff 125 | } 126 | --- 127 | ####Objects 128 | For SOAP responses that return a single array item, or when iterating through a response collection, the MagnetoObject is used. This object comes with a couple methods that should be familiar to a Magento developer 129 | 130 | **getData(optional $key)** - Either returns all of an objects values or a single value that matches the provided key. 131 | 132 | **getId()** - Returns the primary key of a given response object 133 | 134 | Like a **Varien_Object** you can also use a magic getter to pull information from an object. For example, you can use the following two methods to return the incrementId of an order object 135 | 136 | foreach ( Magento::salesOrderList()->getCollection() as $order ) { 137 | 138 | // with data 139 | echo $order->getData('increment_id') 140 | 141 | // with magic getter 142 | echo $order->getIncrementId() 143 | } 144 | 145 | --- 146 | ####Single Results 147 | For SOAP responses that return a single value or boolean, results are returned as a string/integer/boolean 148 | 149 | // will return an integer 150 | echo Magento::customerCustomerCreate($customerParams) 151 | 152 | ###Working multiple connections 153 | You have the option to use connections to multiple Magento websites. This can be done by either adding a secondary connection to the package configuration or by creating a connection on the fly. Connections are stored, so once registered, you can continue to use that connection by referencing it's unique identifier or the newly created connection object. 154 | 155 | To create a connection on the fly 156 | 157 | $connection = Magento::createConnection($name, $url, $user, $key, $version) 158 | 159 | To use a stored connection 160 | 161 | // SOAP v2 162 | $orders = Magento::call($connection)->salesOrderList(); 163 | $orders = Magento::call('unique_identifier')->salesOrderList(); 164 | 165 | // SOAP v1 166 | $customers = Magento::get('customers.list', null, $connection) 167 | $customers = Magento::get('customers.list', null, 'unique_identifier') 168 | 169 | To register a connection programmatically 170 | 171 | Magento::createAndRegisterConnection($name, $url, $user, $key, $version) 172 | 173 | To see a list of available connections 174 | 175 | print_r( Magento::getAvailableConnections() ) 176 | 177 | **Setting a primary connection** 178 | Inherently, unless explicitly passed to a SOAP call, the default connection found in the configuration file is used when making all calls. To change which connection is used by default, you can use 179 | 180 | Magento::setPrimaryConnection('unique_identifier') 181 | 182 | // Then use for subsequent calls 183 | Magento::salesOrderList() 184 | 185 | To see the currently primary connection 186 | 187 | echo Magento::getPrimaryConnection() 188 | 189 | To remove a connection from memory 190 | 191 | Magento::unregister('unique_identifier') 192 | 193 | To create a temporary connection 194 | 195 | $connection = Magento::createAndForgetConnection($name, $url, $user, $key, $version) 196 | 197 | // Then reference it in the call 198 | $orders = Magento::call($connection)->salesOrderList() 199 | 200 | ###Helpers 201 | 202 | **Getting Magic Getters for an object/collection** 203 | Though used just to return data, you can use the following to get a list of available functions for a given object or collection 204 | 205 | // For a collection 206 | echo Magento::call()->customerCustomerList()->getFunctions(); 207 | 208 | // For an object 209 | $customer->getFunctions(); 210 | 211 | This will return something like 212 | 213 | Array 214 | ( 215 | [0] => getCustomerId 216 | [1] => getCreatedAt 217 | [2] => getUpdatedAt 218 | [3] => getStoreId 219 | [4] => getWebsiteId 220 | [5] => getCreatedIn 221 | [6] => getEmail 222 | [7] => getFirstname 223 | [8] => getLastname 224 | [9] => getGroupId 225 | [10] => getDob 226 | [11] => getPasswordHash 227 | ) 228 | 229 | **Test your SOAP connection** 230 | Returns a boolean by default, but can return headers by flagging the second parameter as true. 231 | 232 | var_dump( Magento::testConnection('unique_identifier', $returnHeaders = false) ) 233 | 234 | **Get Magento Version** 235 | Returns the build version of either the default connection or the one passed 236 | 237 | // Example return Community 1.9.0.0 238 | var_dump( Magento::getMagentoVersion(optional $connection) ) 239 | 240 | ###XML Support 241 | Currently, there is no support of XML request and responses. But, it's planned for future releases. 242 | 243 | ###WS-I Compliance Mode 244 | Currently, there is no way enforce WS-I Compliance Mode. 245 | -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Magento.php: -------------------------------------------------------------------------------- 1 | 41 | * @copyright 2014 TinyRocket 42 | * 43 | */ 44 | 45 | class Magento { 46 | 47 | /** 48 | * @var connections 49 | */ 50 | private $connections; 51 | 52 | /** 53 | * @var client 54 | */ 55 | protected $client; 56 | 57 | /** 58 | * @var forgets 59 | */ 60 | protected $forgets; 61 | 62 | /** 63 | * Construct Magento Instance 64 | * 65 | * @return void 66 | */ 67 | public function __construct(Repository $config) 68 | { 69 | $this->connections = $config->get('magento::connections'); 70 | if ( is_array($this->connections) ) { 71 | $this->batchRegister($this->connections); 72 | } 73 | } 74 | 75 | /** 76 | * __call Magic Method 77 | * 78 | * Allows for SOAP v2 request to be ran directly against 79 | * the class rather than using the 'call' method 80 | * 81 | * @return void 82 | */ 83 | public function __call($method, $args) 84 | { 85 | if ( in_array($method, get_class_methods($this)) ) { 86 | call_user_func($method, $args); 87 | } else { 88 | $clientExecutable = $this->call(); 89 | return call_user_func_array(array($clientExecutable, $method), $args); 90 | } 91 | } 92 | 93 | /** 94 | * Call SOAP V2 Method 95 | */ 96 | public function call($connection = null) 97 | { 98 | if ( !is_array($connection) or is_null($connection) ) { 99 | $connection = !is_null($connection) ? $this->getConnection($connection) : $this->getPrimaryConnection(); 100 | } 101 | return new MagentoSoapClient($connection); 102 | } 103 | 104 | /** 105 | * Get Soap V1 Method 106 | */ 107 | public function get($method, $params = array(), $connection = null) 108 | { 109 | $connection = !is_null($connection) ? $this->getConnection($connection) : $this->getPrimaryConnection(); 110 | $soap = new MagentoSoapClient($connection); 111 | if ( (isset($connection[key($connection)]['version']) && (strtolower($connection[key($connection)]['version']) == 'v1') ) ) { 112 | return $soap->call($method, $params); 113 | } else { 114 | return $soap->__call($method, $params); 115 | } 116 | } 117 | 118 | /** 119 | * Use SOAP Method 120 | */ 121 | public function connection($connection = null) 122 | { 123 | if ( !is_null($connection) ) { 124 | return new MagentoSoapClient($this->getConnection($connection)); 125 | } 126 | throw new MagentoSoapClientException("This [connection] paramenter cannot be left blank"); 127 | } 128 | 129 | /** 130 | * Create Connection 131 | * 132 | * Allows a user to create a connection on the fly 133 | * 134 | * @return array 135 | */ 136 | public function createConnection($name, $url, $user, $key, $version = null) 137 | { 138 | if ( !in_array($name, $this->getConnections()) ) { 139 | return array($name => array( 140 | 'site_url' => $url, 141 | 'user' => $user, 142 | 'key' => $key, 143 | 'version' => $version, 144 | )); 145 | } 146 | throw new MagentoSoapClientException("Connection [$name] already exists. Please choose a different identifier"); 147 | } 148 | 149 | /** 150 | * Create And Register Connection 151 | * 152 | * Allows a user to create and register a connection on the fly 153 | * 154 | * @return array 155 | */ 156 | public function createAndRegisterConnection($name, $url, $user, $key, $version = null) 157 | { 158 | return $this->register($this->createConnection($name, $url, $user, $key, $version), false); 159 | } 160 | 161 | /** 162 | * Create a temporary connection 163 | * 164 | * Allows a user to create a temporary connection on the fly 165 | * 166 | * @return array 167 | */ 168 | public function createAndForgetConnection($name, $url, $user, $key, $version = null) 169 | { 170 | if ( $this->forgets[] = $name ) { 171 | return $this->register($this->createConnection($name, $url, $user, $key, $version), false); 172 | } 173 | } 174 | 175 | /** 176 | * Get Connections 177 | * 178 | * @return array 179 | */ 180 | public function getConnections() 181 | { 182 | return $this->connections; 183 | } 184 | 185 | /** 186 | * Get Services 187 | */ 188 | public function getAvailableConnections() 189 | { 190 | return \MagentoSoapStorage::services(); 191 | } 192 | 193 | /** 194 | * Get Primary Connection 195 | * 196 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 197 | */ 198 | public function getPrimaryConnection() 199 | { 200 | return $this->getConnection(\MagentoSoapStorage::primary()); 201 | } 202 | 203 | /** 204 | * Test SOAP Connection 205 | * 206 | * Returns either a boolean reponse or response headers, depending 207 | * on whether $showHeaders is set to true. 208 | * 209 | * @return mixed 210 | */ 211 | public function testConnection($connection = null, $showHeaders = false) 212 | { 213 | if ( !is_null($connection) ) { 214 | $connection = is_array($connection) ? $connection : $this->getConnection($connection, false); 215 | return \MagentoSoapClient::testConnection($connection, $showHeaders); 216 | } 217 | throw new InvalidConnectionException("No connection provided to test. Please provide a connection object or identifier"); 218 | } 219 | 220 | /** 221 | * Get Magento Version 222 | * 223 | * Returns the Magento build version for either the default 224 | * connection or the connection passed through to the function 225 | * 226 | * @return string 227 | * @example 228 | */ 229 | public function getMagentoVersion($connection = null) 230 | { 231 | $connection = is_array($connection) ? $connection : $this->getConnection($connection); 232 | $temporaryClient = new MagentoSoapClient($connection); 233 | return $temporaryClient->getMagentoVersion(); 234 | } 235 | 236 | /** 237 | * Get Primary Connection 238 | * 239 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 240 | */ 241 | public function setPrimaryConnection($name) 242 | { 243 | return $this->getConnection(\MagentoSoapStorage::primary($name)); 244 | } 245 | 246 | /** 247 | * Get Connection 248 | * 249 | * @return connection array 250 | */ 251 | public function getConnection($identifier, $useDefault = true) 252 | { 253 | if ( is_array($identifier) ) { 254 | $identifier = key($identifier); 255 | } 256 | 257 | // Fallback if applicable 258 | if ( $useDefault ) { 259 | $identifier = array_key_exists($identifier, $this->connections) ? $identifier : 'default'; 260 | } 261 | 262 | // Return Connection 263 | if ( isset($this->connections[$identifier]) ) { 264 | return array($identifier => $this->connections[$identifier]); 265 | } 266 | throw new InvalidConnectionException("Connection [$identifier] not found. No default configuration found."); 267 | } 268 | 269 | /** 270 | * Register Connection 271 | * 272 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 273 | */ 274 | public function register($connection, $return = true, $forget = false) 275 | { 276 | try { 277 | $connection = is_array($connection) ? $connection : $this->getConnection($connection); 278 | \MagentoSoapStorage::add($connection); 279 | 280 | if ( true === $forget ) { 281 | $this->forgets[] = key($connection); 282 | } 283 | 284 | return $return ? $this->call($connection) : null; 285 | } catch (Exception $e) { 286 | throw new MagentoSoapClientException($e->getMessage()); 287 | } 288 | } 289 | 290 | /** 291 | * Register Connection 292 | * 293 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 294 | */ 295 | public function batchRegister($connections) 296 | { 297 | try { 298 | foreach ( $connections as $connection => $data ) { 299 | \MagentoSoapStorage::add(array($connection => $data)); 300 | } 301 | } catch (Exception $e) { 302 | throw new MagentoSoapClientException($e->getMessage()); 303 | } 304 | } 305 | 306 | /** 307 | * Get Functions 308 | * 309 | * Extension of the __getFunctions method core to SoapClient 310 | * 311 | * @return array 312 | */ 313 | public function getFunctions($connection = null) 314 | { 315 | return $this->call()->getFunctions(); 316 | } 317 | 318 | /** 319 | * Unregister Connection 320 | * 321 | * @return void 322 | */ 323 | public function unregister($connection) 324 | { 325 | $connection = is_array($connection) ? $connection : $this->getConnection($connection); 326 | \MagentoSoapStorage::remove($this->getConnection($connection)); 327 | } 328 | 329 | 330 | /** 331 | * Has Default Connection 332 | * 333 | * @return bool 334 | */ 335 | public function hasDefaultConnection() 336 | { 337 | return array_key_exists('default', $this->getConnections()); 338 | } 339 | 340 | /** 341 | * Deconstructor 342 | * 343 | * @return void 344 | */ 345 | public function __destruct() { 346 | if ( !is_null($this->forgets) && is_array($this->forgets) ) { 347 | foreach ( $this->forgets as $removeKey ) { 348 | $this->unregister($removeKey); 349 | } 350 | 351 | $this->forgets = null; 352 | } 353 | } 354 | } --------------------------------------------------------------------------------