Advertisement
Guest User

Esemeser\Api\Simple\Sender.php

a guest
Nov 11th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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\Sender 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 Sender {
  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.      * Recipient name (max 80 signs length)
  48.      * @var string
  49.      */
  50.     private $_recipientName;
  51.  
  52.     /**
  53.      * Recipient phone number (exactly nine digits)
  54.      * @var string
  55.      */
  56.     private $_recipientNumber;
  57.  
  58.     /**
  59.      * Message body (max 160 signs).
  60.      * @var string
  61.      */
  62.     private $_message;
  63.  
  64.     /**
  65.      * Array of allowed message types.
  66.      * @var array
  67.      */
  68.     private $_allowedTypes = array(
  69.         'standard',
  70.         'plus'
  71.     );
  72.  
  73.     /**
  74.      * Checks if the method curl_init exists and if not it throw exception.
  75.      * @throws \BadFunctionCallException
  76.      */
  77.     public function __construct() {
  78.  
  79.         if (!function_exists('curl_init')) {
  80.             throw new \BadFunctionCallException('Can\'t find CURL library');
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Setting for sender
  86.      * @param string
  87.      * @param string
  88.      * @param string
  89.      * @throws \InvalidArgumentException
  90.      */
  91.     public function setSender($accountName, $login, $password) {
  92.  
  93.         if (empty($accountName) || empty($login) || empty($password)) {
  94.             throw new \InvalidArgumentException('You must specify account name and password');
  95.         }
  96.  
  97.         $this->_account = $accountName;
  98.         $this->_login = $login;
  99.         $this->_password = $password;
  100.     }
  101.  
  102.     /**
  103.      * Set type of message
  104.      * @param string
  105.      * @throws \InvalidArgumentException
  106.      */
  107.     public function setType($type) {
  108.  
  109.         if (!in_array($type, $this->_allowedTypes)) {
  110.             throw new \InvalidArgumentException('Unsupported message type');
  111.         }
  112.  
  113.         $this->_type = $type;
  114.     }
  115.  
  116.     /**
  117.      * Set number and name of recipient
  118.      * @param string
  119.      * @param number
  120.      * @throws \InvalidArgumentException
  121.      */
  122.     public function setRecipient($recipientName, $recipientNumber) {
  123.  
  124.         if (!preg_match('/^[0-9]{9}$/', $recipientNumber)) {
  125.             throw new \InvalidArgumentException('Incorrect number');
  126.         }
  127.  
  128.         if (mb_strlen($recipientName) > 80) {
  129.             throw new \InvalidArgumentException('Recipient name is too long');
  130.         }
  131.  
  132.         $this->_recipientName = $recipientName;
  133.         $this->_recipientNumber = $recipientNumber;
  134.     }
  135.  
  136.     /**
  137.      * Set message content, this method also replace prohibited chars like ' " /
  138.      * if second parameter $autoTrim is true, the message will be truncated to 160 characters automatically otherwise method throws Exception if message will be longer than 160 signs
  139.      *
  140.      * @param string $message
  141.      * @param boolean $autoTrim
  142.      * @throws \InvalidArgumentException
  143.      */
  144.     public function setMessage($message, $autoTrim = false) {
  145.  
  146.         $prohibitedChars = array(
  147.             '\'',
  148.             '"',
  149.             '/'
  150.         );
  151.  
  152.         $replacement = array(
  153.             ' ',
  154.             ' ',
  155.             '\\'
  156.         );
  157.  
  158.         if (mb_strlen($message) > 160 && $autoTrim == false) {
  159.             throw new \InvalidArgumentException('Message is too long');
  160.         } else if (mb_strlen($message) > 160 && $autoTrim == true) {
  161.             $message = mb_substr($message, 0, 160);
  162.         }
  163.  
  164.         $this->_message = str_replace($prohibitedChars, $replacement, $message);
  165.     }
  166.  
  167.     /**
  168.      * Send message and return true or throw exception
  169.      * @return boolean
  170.      * @throws \BadMethodCallException
  171.      * @throws \InvalidArgumentException
  172.      * @throws \RuntimeException
  173.      */
  174.     public function send() {
  175.        
  176.         //TODO: Add choice if we want using https or http
  177.  
  178.         if (empty($this->_account) || empty($this->_login) || empty($this->_password) || empty($this->_recipientNumber)) {
  179.  
  180.             throw new \BadMethodCallException('You do it wrong! please read examples files first.');
  181.         }
  182.  
  183.         $requestVars = array();
  184.  
  185.         $requestVars['konto'] = $this->_account;
  186.         $requestVars['login'] = $this->_login;
  187.         $requestVars['haslo'] = $this->_password;
  188.  
  189.         if (!empty($this->_type)) {
  190.  
  191.             $requestVars['rodzaj'] = $this->_type;
  192.         }
  193.  
  194.         $requestVars['nazwa'] = $this->_recipientName;
  195.         $requestVars['telefon'] = $this->_recipientNumber;
  196.         $requestVars['tresc'] = $this->_message;
  197.        
  198.         //init curl and send request
  199.         $ch = curl_init('http://esemeser.pl/0api/wyslij.php');
  200.        
  201.         curl_setopt($ch, CURLOPT_POST,1);
  202.         curl_setopt($ch, CURLOPT_POSTFIELDS,  http_build_query($requestVars));
  203.         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  204.         curl_setopt($ch, CURLOPT_FAILONERROR,true);
  205.        
  206.         $response =  curl_exec($ch);
  207.  
  208.         if ($response === false){
  209.            
  210.             $errormsg=curl_error($ch);
  211.             curl_close($ch);
  212.             throw new \RuntimeException($errormsg);
  213.            
  214.         }
  215.        
  216.         curl_close($ch);
  217.        
  218.         switch ($response){
  219.            
  220.             case 'OK':
  221.                 return true;
  222.                 break;            
  223.             case '-1':
  224.                 throw new \InvalidArgumentException('Account '.$this->_account.' does not exists');
  225.                 break;
  226.             case '-2':
  227.                 throw new \InvalidArgumentException('Incorrect login or password');
  228.                 break;
  229.             case '-3':
  230.                 throw new \InvalidArgumentException('Incorrect recipient number');
  231.                 break;
  232.             case 'NIE':
  233.             default:
  234.                 throw new \RuntimeException('Message has not been sent from unknown reasons');    
  235.                
  236.         }
  237.        
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement