Skip to content

Commit

Permalink
improve refactoring of smsapi functions (ltb-project#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Apr 25, 2024
1 parent df4ad17 commit 73a7a10
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 51 deletions.
4 changes: 2 additions & 2 deletions docs/config_sms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ In this library, you must define a class with the ``send_sms_by_api`` function:
class smsSignal
{
public $param1;
public $param2;
private $param1;
private $param2;
public function __construct($param1, $param2)
{
Expand Down
6 changes: 3 additions & 3 deletions docs/sms_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Provider website: https://www.linkmobility.com/
class smsLink
{
public $api_username;
public $api_password;
public $SenderName;
private $api_username;
private $api_password;
private $SenderName;
public function __construct($api_username, $api_password, $SenderName)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/smsapi-example.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
class smsExample
{

public $example_param1;
public $example_param2;
private $example_param1;
private $example_param2;

public function __construct($example_param1, $example_param2)
{
Expand Down
6 changes: 3 additions & 3 deletions lib/smsapi-signal-cli.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
class smsSignal
{

public $signal_user;
public $signal_config;
public $signal_cli;
private $signal_user;
private $signal_config;
private $signal_cli;

public function __construct($signal_user, $signal_config, $signal_cli)
{
Expand Down
8 changes: 4 additions & 4 deletions lib/smsapi-twilio.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
class smsTwilio
{

public $twilio_sid;
public $twilio_auth_token;
public $twilio_outgoing_number;
public $twilio_lookup_first;
private $twilio_sid;
private $twilio_auth_token;
private $twilio_outgoing_number;
private $twilio_lookup_first;

public function __construct($twilio_sid, $twilio_auth_token, $twilio_outgoing_number, $twilio_lookup_first)
{
Expand Down
25 changes: 11 additions & 14 deletions lib/smsapi.inc.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

function create_instance($class, $params) {
$reflection_class = new ReflectionClass($class);
return $reflection_class->newInstanceArgs($params);
}

function createSMSInstance($sms_api_lib, $config)
{
# Get all loaded classes
Expand All @@ -23,21 +18,23 @@ function createSMSInstance($sms_api_lib, $config)
exit(1);
}

# Inspect class properties of sms modules
$class_vars = get_class_vars($class);
# Inspect parameters of constructor
$reflection = new ReflectionClass($class);
$constructorParams = $reflection->getConstructor()->getParameters();

# Gather properties to pass to the class: all config params to pass to sms module
# Gather parameters to pass to the sms class: all config params to pass to sms module
$params = [];
foreach (array_keys($class_vars) as $param)
{
if(!isset($config[$param]))
foreach ($constructorParams AS $param) {
if(!isset($config[$param->name]))
{
error_log("Error: Missing param $param in $sms_api_lib");
error_log("Error: Missing param $param->name in $sms_api_lib");
exit(1);
}
array_push($params, $config[$param]);
array_push($params, $config[$param->name]);
}
return create_instance($class, $params);

# return new instance of class, passing all grabbed parameters
return new $class(...$params);
}

?>
8 changes: 4 additions & 4 deletions lib/smsovh/smsapi-ovh.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
class smsOVH
{

public $ovh_appkey;
public $ovh_appsecret;
public $ovh_consumerkey;
public $ovh_smssender;
private $ovh_appkey;
private $ovh_appsecret;
private $ovh_consumerkey;
private $ovh_smssender;

public function __construct($ovh_appkey, $ovh_appsecret, $ovh_consumerkey, $ovh_smssender)
{
Expand Down
5 changes: 0 additions & 5 deletions tests/smsOVHTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public function test_send_sms_by_api_ovh(): void
$ovh_consumerkey,
$ovh_smssender);

$this->assertEquals("$ovh_appkey", $smsInstance->ovh_appkey, "Bad appkey");
$this->assertEquals("$ovh_appsecret", $smsInstance->ovh_appsecret, "Bad appsecret");
$this->assertEquals("$ovh_consumerkey", $smsInstance->ovh_consumerkey, "Bad consumerkey");
$this->assertEquals("$ovh_smssender", $smsInstance->ovh_smssender, "Bad smssender");

$res = $smsInstance->send_sms_by_api($mobile, $message);

$this->assertEquals(0, $res, "Wrong return code by ovh send_sms_by_api function");
Expand Down
4 changes: 0 additions & 4 deletions tests/smsSignalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ function ($command, &$output, &$return_var) {
$signal_config,
$signal_cli);

$this->assertEquals("$signal_user", $smsInstance->signal_user, "Bad signal user");
$this->assertEquals("$signal_config", $smsInstance->signal_config, "Bad signal config");
$this->assertEquals("$signal_cli", $smsInstance->signal_cli, "Bad signal cli");

$res = $smsInstance->send_sms_by_api($mobile, $message);

$this->assertEquals(1, $res, "Wrong return code by send_sms_by_api function");
Expand Down
10 changes: 0 additions & 10 deletions tests/smsTwilioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ function ($ch) {
$twilio_outgoing_number,
$twilio_lookup_first);

$this->assertEquals("$twilio_sid", $smsInstance->twilio_sid, "Bad sid");
$this->assertEquals("$twilio_auth_token", $smsInstance->twilio_auth_token, "Bad auth_token");
$this->assertEquals("$twilio_outgoing_number", $smsInstance->twilio_outgoing_number, "Bad outgoing_number");
$this->assertEquals($twilio_lookup_first, $smsInstance->twilio_lookup_first, "Bad lookup_first");

$res = $smsInstance->send_sms_by_api($mobile, $message);

$this->assertEquals(1, $res, "Wrong return code by twilio send_sms_by_api function");
Expand Down Expand Up @@ -161,11 +156,6 @@ function ($ch) {
$twilio_outgoing_number,
$twilio_lookup_first);

$this->assertEquals("$twilio_sid", $smsInstance->twilio_sid, "Bad sid");
$this->assertEquals("$twilio_auth_token", $smsInstance->twilio_auth_token, "Bad auth_token");
$this->assertEquals("$twilio_outgoing_number", $smsInstance->twilio_outgoing_number, "Bad outgoing_number");
$this->assertEquals($twilio_lookup_first, $smsInstance->twilio_lookup_first, "Bad lookup_first");

$res = $smsInstance->send_sms_by_api($mobile, $message);

$this->assertEquals(0, $res, "Wrong return code by twilio send_sms_by_api function during first lookup");
Expand Down

0 comments on commit 73a7a10

Please sign in to comment.