      1: <?php
      2:
      3: namespace SMSApi\Api\Action;
      4:
      5: /**
      6:  * Class AbstractAction
      7:  * @package SMSApi\Api\Action
      8:  */
      9: abstract class AbstractAction {
     10:
     11:     /**
     12:      * @var
     13:      */
     14:     protected $client;
     15:     /**
     16:      * @var
     17:      */
     18:     protected $proxy;
     19:     /**
     20:      * @var array
     21:      */
     22:     protected $params = array( );
     23:     /**
     24:      * @var \ArrayObject
     25:      */
     26:     protected $to;
     27:     /**
     28:      * @var \ArrayObject
     29:      */
     30:     protected $idx;
     31:     /**
     32:      * @var
     33:      */
     34:     protected $group;
     35:     /**
     36:      * @var
     37:      */
     38:     protected $date;
     39:     /**
     40:      * @var
     41:      */
     42:     protected $encoding;
     43:
     44:     /**
     45:      *
     46:      */
     47:     function __construct() {
     48:         $this->to = new \ArrayObject();
     49:         $this->idx = new \ArrayObject();
     50:     }
     51:
     52:     /**
     53:      * @return mixed
     54:      */
     55:     abstract public function uri();
     56:
     57:     /**
     58:      * @param $data
     59:      * @return mixed
     60:      */
     61:     abstract protected function response( $data );
     62:
     63:     /**
     64:      * @return null
     65:      */
     66:     public function file() {
     67:         return null;
     68:     }
     69:
     70:     /**
     71:      * @param \SMSApi\Client $client
     72:      */
     73:     public function client( \SMSApi\Client $client ) {
     74:         $this->client = $client;
     75:     }
     76:
     77:     /**
     78:      * @param \SMSApi\Proxy\Proxy $proxy
     79:      */
     80:     public function proxy( \SMSApi\Proxy\Proxy $proxy ) {
     81:         $this->proxy = $proxy;
     82:     }
     83:
     84:     /**
     85:      * @param $val
     86:      * @return $this
     87:      */
     88:     public function setTest( $val ) {
     89:         if ( $val == true ) {
     90:             $this->params[ 'test' ] = 1;
     91:         } else if ( $val == false ) {
     92:             unset( $this->params[ 'test' ] );
     93:         }
     94:
     95:         return $this;
     96:     }
     97:
     98:     /**
     99:      * @param $val
    100:      * @return $this
    101:      */
    102:     protected function setJson( $val ) {
    103:         if ( $val == true ) {
    104:             $this->params[ 'format' ] = 'json';
    105:         } else if ( $val == false ) {
    106:             unset( $this->params[ 'format' ] );
    107:         }
    108:
    109:         return $this;
    110:     }
    111:
    112:     /**
    113:      * @param string $skip
    114:      * @return string
    115:      */
    116:     protected function paramsOther( $skip = "" ) {
    117:
    118:         $query = "";
    119:
    120:         foreach ( $this->params as $key => $val ) {
    121:             if ( $key != $skip && $val != null ) {
    122:                 $query .= '&' . $key . '=' . $val;
    123:             }
    124:         }
    125:
    126:         return $query;
    127:     }
    128:
    129:     /**
    130:      * @return string
    131:      * @throws \SMSApi\Exception\ActionException
    132:      */
    133:     protected function renderTo() {
    134:
    135:         $sizeTo = $this->to->count();
    136:         $sizeIdx = $this->idx->count();
    137:
    138:         if ( $sizeIdx > 0 ) {
    139:             if ( ($sizeTo != $sizeIdx ) ) {
    140:                 throw new \SMSApi\Exception\ActionException( "size idx is not equals to" );
    141:             } else {
    142:                 return $this->renderList( $this->to, ',' ) . "&idx=" . $this->renderList( $this->idx, '|' );
    143:             }
    144:         }
    145:
    146:         return $this->renderList( $this->to, ',' );
    147:     }
    148:
    149:     /**
    150:      * @param \ArrayObject $values
    151:      * @param $delimiter
    152:      * @return string
    153:      */
    154:     private function renderList( \ArrayObject $values, $delimiter ) {
    155:
    156:         $query = "";
    157:         $loop = 1;
    158:         $size = $values->count();
    159:
    160:         foreach ( $values as $val ) {
    161:             $query .= $val;
    162:             if ( $loop < $size ) {
    163:                 $query .= $delimiter;
    164:             }
    165:
    166:             $loop++;
    167:         }
    168:
    169:         return $query;
    170:     }
    171:
    172:     /**
    173:      * @return string
    174:      * @throws \SMSApi\Exception\ActionException
    175:      */
    176:     protected function paramsBasicToQuery() {
    177:
    178:         $query = "";
    179:
    180:         $query .= ($this->group != null) ? "&group=" . $this->group : "&to=" . $this->renderTo();
    181:
    182:         $query .= ($this->date != null) ? "&date=" . $this->date : "";
    183:
    184:         $query .= ( $this->encoding != null ) ? "&encoding=" . $this->encoding : "";
    185:
    186:         return $query;
    187:     }
    188:
    189:     /**
    190:      * @return string
    191:      */
    192:     protected function paramsLoginToQuery() {
    193:         return "username=" . $this->client->getUsername() . "&password=" . $this->client->getPassword();
    194:     }
    195:
    196:     /**
    197:      * @return mixed
    198:      * @throws \SMSApi\Exception\ClientException
    199:      * @throws \SMSApi\Exception\ActionException
    200:      * @throws \SMSApi\Exception\HostException
    201:      */
    202:     public function execute()
    203:     {
    204:         try
    205:         {
    206:             $this->setJson( true );
    207:
    208:             $data = $this->proxy->execute( $this );
    209:
    210:             $this->handleError( $data );
    211:
    212:             return $this->response( $data );
    213:         }
    214:         catch ( Exception $ex )
    215:         {
    216:             throw new \SMSApi\Exception\ActionException( $ex->getMessage() );
    217:         }
    218:     }
    219:
    220:     /**
    221:      * @param $data
    222:      * @throws \SMSApi\Exception\ActionException
    223:      * @throws \SMSApi\Exception\ClientException
    224:      * @throws \SMSApi\Exception\HostException
    225:      */
    226:     protected function handleError( $data ) {
    227:
    228:         $error = new \SMSApi\Api\Response\ErrorResponse( $data );
    229:
    230:         if ( $error->isError() ) {
    231:             if ( \SMSApi\Exception\SmsapiException::isHostError( $error->code ) ) {
    232:                 throw new \SMSApi\Exception\HostException( $error->message, $error->code );
    233:             }
    234:
    235:             if ( \SMSApi\Exception\SmsapiException::isClientError( $error->code ) ) {
    236:                 throw new \SMSApi\Exception\ClientException( $error->message, $error->code );
    237:             } else {
    238:                 throw new \SMSApi\Exception\ActionException( $error->message, $error->code );
    239:             }
    240:         }
    241:     }
    242:
    243: }