Advertisement
Guest User

Esemeser\Api\Simple\Balance.php

a guest
Nov 11th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Esemeser\Api\Simple;
  4.  
  5. /**
  6.  * Class to handle the api provided by esemeser.pl
  7.  *
  8.  * This class based on document: http://esemeser.pl/0pdf/api-dokumentacja.pdf and use CURL to make request.
  9.  * Another way to do this is using Esemeser\Api\Soap\Balance class which using SOAP to communicate with API.
  10.  *
  11.  * @category Esemeser
  12.  * @package API
  13.  * @subpackage Simple
  14.  * @version 0.9
  15.  * @since 10.11.2012
  16.  * @author d3ut3r
  17.  */
  18.  
  19. class Balance {
  20.  
  21.     /**
  22.      * Account name on esemeser.pl
  23.      * @var string
  24.      */
  25.     private $_account;
  26.  
  27.     /**
  28.      * Account login
  29.      * @var string
  30.      */
  31.     private $_login;
  32.  
  33.     /**
  34.      * Account password
  35.      * @var string
  36.      */
  37.     private $_password;
  38.  
  39.     /**
  40.      * Type of message.
  41.      * This variable can contain one of $_allowedTypes values
  42.      * @var string
  43.      */
  44.     private $_type;
  45.  
  46.     /**
  47.      * Array of allowed message types. Type "all" allows you to check the number of messages of all types
  48.      * @var array
  49.      */
  50.     private $_allowedTypes = array(
  51.         'standard',
  52.         'plus',
  53.         'all'
  54.     );
  55.  
  56.     /**
  57.      * Checks if the method curl_init exists and if not it throw exception.
  58.      * @throws \BadFunctionCallException
  59.      */
  60.     public function __construct() {
  61.  
  62.         if (!function_exists('curl_init')) {
  63.             throw new \BadFunctionCallException('Can\'t find CURL library');
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Setting for account
  69.      * @param string
  70.      * @param string
  71.      * @param string
  72.      * @throws \InvalidArgumentException
  73.      */
  74.     public function setAccount($accountName, $login, $password) {
  75.  
  76.         if (empty($accountName) || empty($login) || empty($password)) {
  77.             throw new \InvalidArgumentException('You must specify account name and password');
  78.         }
  79.  
  80.         $this->_account = $accountName;
  81.         $this->_login = $login;
  82.         $this->_password = $password;
  83.     }
  84.  
  85.     /**
  86.      * Set type of message
  87.      * @param string
  88.      * @throws \InvalidArgumentException
  89.      */
  90.     public function setType($type) {
  91.  
  92.         if (!in_array($type, $this->_allowedTypes)) {
  93.             throw new \InvalidArgumentException('Unsupported message type');
  94.         }
  95.  
  96.         $this->_type = $type;
  97.     }
  98.  
  99.     /**
  100.      * Check balance and return integer or array for type "all".
  101.      * @return mixed
  102.      * @throws \BadMethodCallException
  103.      * @throws \InvalidArgumentException
  104.      * @throws \RuntimeException
  105.      */
  106.     public function getBalance() {
  107.  
  108.         //TODO: Add choice if we want using https or http
  109.  
  110.         if (empty($this->_account) || empty($this->_login) || empty($this->_password)) {
  111.  
  112.             throw new \BadMethodCallException('You do it wrong! please read examples files first.');
  113.         }
  114.  
  115.         $requestVars = array();
  116.  
  117.         $requestVars['konto'] = $this->_account;
  118.         $requestVars['login'] = $this->_login;
  119.         $requestVars['haslo'] = $this->_password;
  120.  
  121.         if (!empty($this->_type) && $this->_type != 'all') {
  122.  
  123.             $requestVars['rodzaj'] = $this->_type;
  124.         } else if (!empty($this->_type) && $this->_type == 'all') {
  125.  
  126.             return $this->checkAll();
  127.         }
  128.  
  129.         //init curl and send request
  130.         $ch = curl_init('http://esemeser.pl/0api/sprawdz.php');
  131.  
  132.         curl_setopt($ch, CURLOPT_POST, 1);
  133.         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestVars));
  134.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  135.         curl_setopt($ch, CURLOPT_FAILONERROR, true);
  136.  
  137.         $response = curl_exec($ch);
  138.  
  139.         if ($response === false) {
  140.  
  141.             $errormsg = curl_error($ch);
  142.             curl_close($ch);
  143.             throw new \RuntimeException($errormsg);
  144.         }
  145.  
  146.         curl_close($ch);
  147.  
  148.         switch ($response) {
  149.  
  150.  
  151.             case '-1':
  152.                 throw new \InvalidArgumentException('Account ' . $this->_account . ' does not exists');
  153.                 break;
  154.             case '-2':
  155.                 throw new \InvalidArgumentException('Incorrect login or password');
  156.                 break;
  157.  
  158.             default:
  159.                 return (int) $response;
  160.         }
  161.     }
  162.    
  163.     /**
  164.      * Internal method, create associative array with type and messages count. This method should be used only from getBalance()
  165.      * @return array    
  166.      */
  167.  
  168.     private function checkAll() {
  169.  
  170.         $response = array();
  171.  
  172.         foreach ($this->_allowedTypes as $newType) {
  173.  
  174.             if ($newType == 'all') {
  175.                 continue;
  176.             }
  177.  
  178.             $this->setType($newType);
  179.             $response[$newType] = $this->getBalance();
  180.         }
  181.        
  182.         $this->setType('all');
  183.  
  184.         return $response;
  185.     }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement