From 73a7a104539f43cca53208315fa76028012a4107 Mon Sep 17 00:00:00 2001 From: David Coutadeur Date: Thu, 25 Apr 2024 11:27:39 +0200 Subject: [PATCH] improve refactoring of smsapi functions (#889) --- docs/config_sms.rst | 4 ++-- docs/sms_api.rst | 6 +++--- lib/smsapi-example.inc.php | 4 ++-- lib/smsapi-signal-cli.inc.php | 6 +++--- lib/smsapi-twilio.inc.php | 8 ++++---- lib/smsapi.inc.php | 25 +++++++++++-------------- lib/smsovh/smsapi-ovh.inc.php | 8 ++++---- tests/smsOVHTest.php | 5 ----- tests/smsSignalTest.php | 4 ---- tests/smsTwilioTest.php | 10 ---------- 10 files changed, 29 insertions(+), 51 deletions(-) diff --git a/docs/config_sms.rst b/docs/config_sms.rst index f85441df..510b36b3 100644 --- a/docs/config_sms.rst +++ b/docs/config_sms.rst @@ -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) { diff --git a/docs/sms_api.rst b/docs/sms_api.rst index f5b47430..3d678c9b 100644 --- a/docs/sms_api.rst +++ b/docs/sms_api.rst @@ -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) { diff --git a/lib/smsapi-example.inc.php b/lib/smsapi-example.inc.php index 6e9c69bd..fbde1382 100644 --- a/lib/smsapi-example.inc.php +++ b/lib/smsapi-example.inc.php @@ -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) { diff --git a/lib/smsapi-signal-cli.inc.php b/lib/smsapi-signal-cli.inc.php index 88ca253d..927ea3c4 100644 --- a/lib/smsapi-signal-cli.inc.php +++ b/lib/smsapi-signal-cli.inc.php @@ -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) { diff --git a/lib/smsapi-twilio.inc.php b/lib/smsapi-twilio.inc.php index a8dea448..c04dd774 100644 --- a/lib/smsapi-twilio.inc.php +++ b/lib/smsapi-twilio.inc.php @@ -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) { diff --git a/lib/smsapi.inc.php b/lib/smsapi.inc.php index 865ac8d0..4b36e87e 100644 --- a/lib/smsapi.inc.php +++ b/lib/smsapi.inc.php @@ -1,10 +1,5 @@ newInstanceArgs($params); -} - function createSMSInstance($sms_api_lib, $config) { # Get all loaded classes @@ -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); } ?> diff --git a/lib/smsovh/smsapi-ovh.inc.php b/lib/smsovh/smsapi-ovh.inc.php index 2b9e8f7c..01a05f82 100644 --- a/lib/smsovh/smsapi-ovh.inc.php +++ b/lib/smsovh/smsapi-ovh.inc.php @@ -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) { diff --git a/tests/smsOVHTest.php b/tests/smsOVHTest.php index 3989468d..32846fda 100644 --- a/tests/smsOVHTest.php +++ b/tests/smsOVHTest.php @@ -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"); diff --git a/tests/smsSignalTest.php b/tests/smsSignalTest.php index da865ae3..23460ee6 100644 --- a/tests/smsSignalTest.php +++ b/tests/smsSignalTest.php @@ -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"); diff --git a/tests/smsTwilioTest.php b/tests/smsTwilioTest.php index 75f8dd3b..2be65f45 100644 --- a/tests/smsTwilioTest.php +++ b/tests/smsTwilioTest.php @@ -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"); @@ -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");