SmsService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 84
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A getPlatform() 0 4 1
A setPlatform() 0 6 1
A getApiKey() 0 4 1
A setApiKey() 0 6 1
A sendMessage() 0 34 3
1
<?php
2
/**
3
 * m'Manager | Invoices Management System
4
 * 
5
 * This content is released under the Proprietary License (Proprietary)
6
 *
7
 * Copyright (c) 2017, Eric Claver AKAFFOU - All Rights Reserved
8
 * Unauthorized copying of this file, via any medium is strictly prohibited
9
 * Proprietary and confidential
10
 * 
11
 * @package m'Manager
12
 * @author  Eric Claver AKAFFOU
13
 * @copyright   Copyright (c) 2017, on'Eric Computing, Inc. (https://www.onericcomputing.com/)
14
 * @license https://www.mmanager.fr  Proprietary License
15
 * @link    https://codecanyon.net/item/mmanager-invoices-management-system/19866435?s_rank=1
16
 * @since   Version 1.0.0
17
 * @filesource
18
 */
19
20
namespace Mmanager;
21
22
use Mmanager\Mmanager;
23
use Mmanager\Logger\FileLogger;
24
use Mmanager\Logger\FileLoggerFactory;
25
use Mmanager\Logger\StdoutLogger;
26
use Mmanager\Logger\StdoutLoggerFactory;
27
28
class SmsService {
29
	protected $platform;
30
  	protected $api_key;
31
32
  	public function __construct($platform = null, $api_key = null) {
33
34
		$this->platform = isset($platform) ? $platform : null;
35
		$this->api_key = isset($api_key) ? $api_key : null;
36
  	}
37
	/**
38
	 * @return mixed
39
	 */
40
	public function getPlatform()
41
	{
42
		return $this->platform;
43
	}
44
45
	/**
46
	 * @param mixed $platform
47
	 *
48
	 * @return self
49
	 */
50
	public function setPlatform($platform)
51
	{
52
		$this->platform = $platform;
53
54
		return $this;
55
	}
56
57
	/**
58
	 * @return mixed
59
	 */
60
	public function getApiKey()
61
	{
62
		return $this->api_key;
63
	}
64
65
	/**
66
	 * @param mixed $api_key
67
	 *
68
	 * @return self
69
	 */
70
	public function setApiKey($api_key)
71
	{
72
		$this->api_key = $api_key;
73
74
		return $this;
75
	}
76
77
	public function sendMessage($phone_number, $message) {
78
		$platform = $this->platform;
79
		$api_key = $this->api_key;
80
81
		$message = urlencode($message);
82
83
		switch ($platform) {
84
			case 'clickatell':
85
				$url = "https://platform.clickatell.com/messages/http/send?apiKey=".$api_key."&to=".$phone_number."&content=".$message;
86
				break;
87
		}
88
		// Get cURL resource
89
		$curl = curl_init();
90
		// Set some options - we are passing in a useragent too here
91
		curl_setopt_array($curl, array(
92
			CURLOPT_RETURNTRANSFER => 1,
93
			CURLOPT_URL => $url
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
94
		));
95
		// Send the request & save response to $resp
96
		$resp = json_decode(curl_exec($curl), true);
97
98
		$log_msg = json_encode(
99
			array(
100
				'time' => date('Y-m-d h:i:sa'),
101
				'to' => $resp['messages'][0]['to'],
102
				'status' => 1 == $resp['messages'][0]['accepted'] ? "success" : "failed",
103
				'errorCode' => $resp['messages'][0]['errorCode'],
104
				'errorDescription' => $resp['messages'][0]['errorDescription']
105
			)
106
		);
107
		Mmanager::write_log($log_msg, "logs/sms", 'json');
108
		// Close request to clear up some resources
109
		curl_close($curl);
110
	}
111
}
112