add adapters

This commit is contained in:
osaajani 2019-11-12 05:18:32 +01:00
parent b1c4a1e3aa
commit f609fc175f
3 changed files with 212 additions and 18 deletions

View File

@ -1,5 +1,5 @@
<?php
namespace \adapters;
namespace adapters;
/**
* Interface for phones adapters
@ -10,47 +10,40 @@
*/
interface AdapterInterface
{
/**
* Classname of the adapter
*/
public static function meta_classname() : string;
/**
* Name of the adapter.
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.)
*/
public const $name;
public static function meta_name() : string;
/**
* Description of the adapter.
* A short description of the service the adapter implements.
*/
public const $description;
public static function meta_description() : string;
/**
* Description of the datas expected by the adapter to help the user. (e.g : A list of expecteds Api credentials fields, with name and value)
*/
public const $datas_help;
public static function meta_datas_help() : string;
/**
* Does the implemented service support flash smss
*/
public const $support_flash;
public static function meta_support_flash() : bool;
/**
* Phone number using the adapter
*/
private $number;
/**
* Datas used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
*/
private $datas;
/**
* Adapter constructor, called when instanciated by RaspiSMS
* @param string $number : Phone number the adapter is used for
* @param json string $datas : JSON string of the datas to configure interaction with the implemented service
* @return bool : True on successfull creation and connection, False on error
*/
public function __construct (string $number, string $datas) : boolean;
public function __construct (string $number, string $datas);
/**

View File

@ -0,0 +1,86 @@
<?php
namespace adapters;
/**
* Interface for phones adapters
* Phone's adapters allow RaspiSMS to use a platform to communicate with a phone number.
* Its an adapter between internal and external code, as an API, command line software, physical modem, etc.
*
* All Phone Adapters must implement this interface
*/
class OvhSmsAdapter implements AdapterInterface
{
/**
* Classname of the adapter
*/
public static function meta_classname() : string { return __CLASS__; }
/**
* Name of the adapter.
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.)
*/
public static function meta_name() : string { return 'OVH SMS'; }
/**
* Description of the adapter.
* A short description of the service the adapter implements.
*/
public static function meta_description() : string { return 'Solution de SMS proposé par le groupe OVH, https://www.ovhtelecom.fr/sms/.'; }
/**
* Description of the datas expected by the adapter to help the user. (e.g : A list of expecteds Api credentials fields, with name and value)
*/
public static function meta_datas_help() : string { return 'See later.'; }
/**
* Does the implemented service support flash smss
*/
public static function meta_support_flash() : bool { return false ; }
/**
* Phone number using the adapter
*/
private $number;
/**
* Datas used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
*/
private $datas;
/**
* Adapter constructor, called when instanciated by RaspiSMS
* @param string $number : Phone number the adapter is used for
* @param json string $datas : JSON string of the datas to configure interaction with the implemented service
*/
public function __construct (string $number, string $datas)
{
$this->number = $number;
$this->datas = $datas;
}
/**
* Method called to send a SMS to a number
* @param string $destination : Phone number to send the sms to
* @param string $text : Text of the SMS to send
* @param bool $flash : Is the SMS a Flash SMS
* @return bool : True if send, False else
*/
public function send (string $destination, string $text, boolean $flash) : boolean
{
return true;
}
/**
* Method called to read SMSs of the number
* @param float $since : Unix microtime representation of the date from wich we want to read the SMSs
* @return array : Array of the sms reads
*/
public function read (float $since) : array
{
return [];
}
}

115
controllers/internals/Adapter.php Executable file
View File

@ -0,0 +1,115 @@
<?php
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace controllers\internals;
/**
* Class to interact with adapters
*/
class Adapter extends \descartes\InternalController
{
private const ADAPTERS_FILES_END = 'Adapter.php';
private const ADAPTERS_META_START = 'meta_';
/**
* List adapters using internal metas
* @return array
*/
public function list_adapters ()
{
$adapters = [];
$files = $this->list_files();
if (!$files)
{
return $adapters;
}
foreach ($files as $file)
{
$metas = $this->read_adapter_metas($file);
if (!$metas)
{
continue;
}
$adapters[] = $metas;
}
return $adapters;
}
/**
* List Adapters files
* @return mixed (false|array) : array of adapters files path
*/
public function list_files ()
{
if (!is_readable(PWD_ADAPTERS))
{
return false;
}
$adapters_files = [];
$files = scandir(PWD_ADAPTERS);
foreach ($files as $filename)
{
$len = mb_strlen(self::ADAPTERS_FILES_END);
$end = mb_substr($filename, -$len);
if ($end !== self::ADAPTERS_FILES_END)
{
continue;
}
$adapters_files[] = PWD_ADAPTERS . '/' . $filename;
}
return $adapters_files;
}
/**
* Read constants of an adapter
* @return mixed(array|bool) : False on error, array of constants name => value
*/
public function read_adapter_metas ($adapter_file)
{
$metas = [];
if (!is_readable($adapter_file))
{
return false;
}
$adapter_classname = pathinfo($adapter_file, PATHINFO_FILENAME);
$reflection_class = new \ReflectionClass('\adapters\\' . $adapter_classname);
if (!$reflection_class)
{
return false;
}
$methods = $reflection_class->getMethods(\ReflectionMethod::IS_STATIC);
foreach ($methods as $method)
{
$start_with = mb_substr($method->getName(), 0, mb_strlen(self::ADAPTERS_META_START));
if ($start_with !== self::ADAPTERS_META_START)
{
continue;
}
$metas[$method->getName()] = $method->invoke(null);
}
return $metas;
}
}