Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Esemeser\Api\Simple;
- /**
- * Class to handle the api provided by esemeser.pl
- *
- * This class based on document: http://esemeser.pl/0pdf/api-dokumentacja.pdf and use CURL to make request.
- * Another way to do this is using Esemeser\Api\Soap\Balance class which using SOAP to communicate with API.
- *
- * @category Esemeser
- * @package API
- * @subpackage Simple
- * @version 0.9
- * @since 10.11.2012
- * @author d3ut3r
- */
- class Balance {
- /**
- * Account name on esemeser.pl
- * @var string
- */
- private $_account;
- /**
- * Account login
- * @var string
- */
- private $_login;
- /**
- * Account password
- * @var string
- */
- private $_password;
- /**
- * Type of message.
- * This variable can contain one of $_allowedTypes values
- * @var string
- */
- private $_type;
- /**
- * Array of allowed message types. Type "all" allows you to check the number of messages of all types
- * @var array
- */
- private $_allowedTypes = array(
- 'standard',
- 'plus',
- 'all'
- );
- /**
- * Checks if the method curl_init exists and if not it throw exception.
- * @throws \BadFunctionCallException
- */
- public function __construct() {
- if (!function_exists('curl_init')) {
- throw new \BadFunctionCallException('Can\'t find CURL library');
- }
- }
- /**
- * Setting for account
- * @param string
- * @param string
- * @param string
- * @throws \InvalidArgumentException
- */
- public function setAccount($accountName, $login, $password) {
- if (empty($accountName) || empty($login) || empty($password)) {
- throw new \InvalidArgumentException('You must specify account name and password');
- }
- $this->_account = $accountName;
- $this->_login = $login;
- $this->_password = $password;
- }
- /**
- * Set type of message
- * @param string
- * @throws \InvalidArgumentException
- */
- public function setType($type) {
- if (!in_array($type, $this->_allowedTypes)) {
- throw new \InvalidArgumentException('Unsupported message type');
- }
- $this->_type = $type;
- }
- /**
- * Check balance and return integer or array for type "all".
- * @return mixed
- * @throws \BadMethodCallException
- * @throws \InvalidArgumentException
- * @throws \RuntimeException
- */
- public function getBalance() {
- //TODO: Add choice if we want using https or http
- if (empty($this->_account) || empty($this->_login) || empty($this->_password)) {
- throw new \BadMethodCallException('You do it wrong! please read examples files first.');
- }
- $requestVars = array();
- $requestVars['konto'] = $this->_account;
- $requestVars['login'] = $this->_login;
- $requestVars['haslo'] = $this->_password;
- if (!empty($this->_type) && $this->_type != 'all') {
- $requestVars['rodzaj'] = $this->_type;
- } else if (!empty($this->_type) && $this->_type == 'all') {
- return $this->checkAll();
- }
- //init curl and send request
- $ch = curl_init('http://esemeser.pl/0api/sprawdz.php');
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestVars));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FAILONERROR, true);
- $response = curl_exec($ch);
- if ($response === false) {
- $errormsg = curl_error($ch);
- curl_close($ch);
- throw new \RuntimeException($errormsg);
- }
- curl_close($ch);
- switch ($response) {
- case '-1':
- throw new \InvalidArgumentException('Account ' . $this->_account . ' does not exists');
- break;
- case '-2':
- throw new \InvalidArgumentException('Incorrect login or password');
- break;
- default:
- return (int) $response;
- }
- }
- /**
- * Internal method, create associative array with type and messages count. This method should be used only from getBalance()
- * @return array
- */
- private function checkAll() {
- $response = array();
- foreach ($this->_allowedTypes as $newType) {
- if ($newType == 'all') {
- continue;
- }
- $this->setType($newType);
- $response[$newType] = $this->getBalance();
- }
- $this->setType('all');
- return $response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement