Net_SMS
[ class tree: Net_SMS ] [ index: Net_SMS ] [ all elements ]

Source for file Factory.php

Documentation is available at Factory.php

  1. <?php
  2. require_once 'Net/SMS/Exception.php';
  3.  
  4. class Net_SMS_Factory {
  5.  
  6.     /**
  7.      * Attempts to return a concrete Gateway instance based on $driver.
  8.      *
  9.      * @param string $driver  The type of concrete Gateway subclass to return.
  10.      *                         This is based on the gateway driver ($driver).
  11.      *                         The code is dynamically included.
  12.      * @param array $params   A hash containing any additional configuration or
  13.      *                         connection parameters a subclass might need.
  14.      *
  15.      * @return Net_SMS  The newly created concrete Gateway instance or false on
  16.      *                   an error.
  17.      */
  18.     public static function build($driver$params = array())
  19.     {
  20.         include_once 'Net/SMS/' $driver '.php';
  21.         $class 'Net_SMS_' $driver;
  22.  
  23.         if (class_exists($class)) {
  24.             switch ($class{
  25.                 case 'Net_SMS_textmagic_http':
  26.                     if (!extension_loaded('json')) {
  27.                         throw new InvalidArgumentException("JSON extenstion isn't loaded!");
  28.                     }
  29.  
  30.                     $request = new HTTP_Request2();
  31.                     $request->setMethod('POST');
  32.                     $request->setConfig('timeout'5);
  33.                     $request->setConfig('follow_redirects'true);
  34.                     return new Net_SMS_textmagic_http($params$request);
  35.  
  36.                 case 'Net_SMS_clickatell_http':
  37.                     $request = new HTTP_Request2();
  38.                     $request->setMethod('POST');
  39.                     $request->setConfig('timeout'5);
  40.                     $request->setConfig('follow_redirects'true);
  41.                     return new Net_SMS_clickatell_http($params$request);
  42.  
  43.                 case 'Net_SMS_textmagic_http':
  44.                     $request = new HTTP_Request2();
  45.                     $request->setMethod('POST');
  46.                     $request->setConfig('timeout'5);
  47.                     $request->setConfig('follow_redirects'true);
  48.                     return new Net_SMS_textmagic_http($params$request);
  49.  
  50.                 case 'Net_SMS_sms2email_http':
  51.                     $request = new HTTP_Request2();
  52.                     $request->setMethod('POST');
  53.                     $request->setConfig('timeout'5);
  54.                     $request->setConfig('follow_redirects'true);
  55.                     return new Net_SMS_textmagic_http($params$request);
  56.  
  57.                 case 'Net_SMS_generic_smtp':
  58.                    if (!isset($params['mailBackend']|| !isset($params['mailParams'])) {
  59.                         throw new InvalidArgumentException("You must specify a mailBackend, mailHeaders and mailParams as sperate parameters. mailHeaders may be an empty array.");
  60.                     }
  61.  
  62.                     require_once 'Mail.php';
  63.                     $m = Mail::factory($params['mailBackend'],
  64.                                         $params['mailParams']);
  65.  
  66.                     return new Net_SMS_generic_smtp($params$m);
  67.  
  68.                 case 'Net_SMS_vodafoneitaly_smtp':
  69.                    if (!isset($params['mailBackend']|| !isset($params['mailParams'])) {
  70.                         throw new InvalidArgumentException("You must specify a mailBackend and mailParams as sperate parameters");
  71.                     }
  72.                     require_once 'Mail.php';
  73.                     $m = Mail::factory($params['mailBackend'],
  74.                                         $params['mailParams']);
  75.  
  76.                     return new Net_SMS_vodafoneitaly_smtp($params$m);
  77.  
  78.                 case 'Net_SMS_generic_smpp':
  79.                     if (!isset($params['host']|| !isset($params['port'])) {
  80.                         throw new InvalidArgumentException("You must specify a host and port as sperate parameters");
  81.                     }
  82.                     $client = new Net_SMPP_Client($params['host']$params['port']);
  83.  
  84.                     if (isset($params['vendor'])) {
  85.                         /** @todo Assess if this is actually beneficial */
  86.                         Net_SMPP::setVendor($params['vendor']);
  87.                     }
  88.  
  89.                     return new Net_SMS_generic_smpp($params$client);
  90.  
  91.                 default:
  92.                     return new $class($params);
  93.             }
  94.         }
  95.  
  96.         throw new Net_SMS_Exception(sprintf(_("Class definition of %s not found.")$driver));
  97.     }
  98.  
  99.     /**
  100.      * Returns information on a gateway, such as name and a brief description,
  101.      * from the driver subclass getInfo() function.
  102.      *
  103.      * @return array  An array of extra information.
  104.      */
  105.     public static function getGatewayInfo($gateway)
  106.     {
  107.         static $info = array();
  108.         if (isset($info[$gateway])) {
  109.             return $info[$gateway];
  110.         }
  111.  
  112.         require_once 'Net/SMS/' $gateway '.php';
  113.         $class 'Net_SMS_' $gateway;
  114.         $info[$gateway= call_user_func(array($class'getInfo'));
  115.  
  116.         return $info[$gateway];
  117.     }
  118.  
  119.     /**
  120.      * Returns parameters for a gateway from the driver subclass getParams()
  121.      * function.
  122.      *
  123.      * @param string  The name of the gateway driver for which to return the
  124.      *                 parameters.
  125.      *
  126.      * @return array  An array of extra information.
  127.      */
  128.     public static function getGatewayParams($gateway)
  129.     {
  130.         static $params = array();
  131.         if (isset($params[$gateway])) {
  132.             return $params[$gateway];
  133.         }
  134.  
  135.         require_once 'Net/SMS/' $gateway '.php';
  136.         $class 'Net_SMS_' $gateway;
  137.         $params[$gatewaycall_user_func(array($class'getParams'));
  138.  
  139.         return $params[$gateway];
  140.     }
  141.  
  142.     /**
  143.      * Returns a list of available gateway drivers.
  144.      *
  145.      * @return array  An array of available drivers.
  146.      */
  147.     public static function getDrivers()
  148.     {
  149.         static $drivers = array();
  150.         if (!empty($drivers)) {
  151.             return $drivers;
  152.         }
  153.  
  154.         $drivers = array();
  155.  
  156.         if ($driver_dir opendir(dirname(__FILE__'/SMS/')) {
  157.             while (false !== ($file readdir($driver_dir))) {
  158.                 /* Hide dot files and non .php files. */
  159.                 if (substr($file01!= '.' && substr($file-4== '.php'{
  160.                     $driver substr($file0-4);
  161.                     $driver_info = Net_SMS_Factory::getGatewayInfo($driver);
  162.                     $drivers[$driver$driver_info['name'];
  163.                 }
  164.             }
  165.             closedir($driver_dir);
  166.         }
  167.  
  168.         return $drivers;
  169.     }
  170.  
  171.     /**
  172.      * Returns send parameters for a gateway from the driver subclass
  173.      * getDefaultSendParams()function. These are parameters which are available
  174.      * to the user during sending, such as setting a time for delivery, or type
  175.      * of SMS (normal text or flash), or source address, etc.
  176.      *
  177.      * @param string  The name of the gateway driver for which to return the
  178.      *                 send parameters.
  179.      *
  180.      * @return array  An array of available send parameters.
  181.      */
  182.     public static function getDefaultSendParams($gateway)
  183.     {
  184.         static $params = array();
  185.         if (isset($params[$gateway])) {
  186.             return $params[$gateway];
  187.         }
  188.  
  189.         require_once 'Net/SMS/' $gateway '.php';
  190.         $class 'Net_SMS_' $gateway;
  191.         $params[$gatewaycall_user_func(array($class'getDefaultSendParams'));
  192.  
  193.         return $params[$gateway];
  194.     }
  195.  
  196. }

Documentation generated on Mon, 11 Mar 2019 15:57:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.