Semaphore   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 32.18 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 1
cbo 2
dl 28
loc 87
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 31 3
B balance() 28 28 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace ridvanbaluyos\sms\providers;
3
4
use ridvanbaluyos\sms\SmsProviderServicesInterface as SmsProviderServicesInterface;
5
use ridvanbaluyos\sms\Sms as Sms;
6
use Noodlehaus\Config as Config;
7
use Exception as Exception;
8
9
/**
10
 * Class Semaphore
11
 * @package ridvanbaluyos\sms\providers
12
 */
13
class Semaphore extends Sms implements SmsProviderServicesInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $className;
19
20
    /**
21
     * Semaphore constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->className = substr(get_called_class(), strrpos(get_called_class(), '\\') + 1);
26
    }
27
28
    /**
29
     * This function sends the SMS.
30
     *
31
     * @param $phoneNumber
32
     * @param $message
33
     * @return string
34
     */
35
    public function send($phoneNumber, $message)
36
    {
37
        try {
38
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
39
            $query = [
40
                'from' => $conf['from'],
41
                'api' => $conf['api'],
42
                'number' => $phoneNumber,
43
                'message' => $message,
44
            ];
45
46
            $url = $conf['url'] . '?' . http_build_query($query);
47
48
            $ch = curl_init();
49
            curl_setopt($ch,CURLOPT_URL,$url);
50
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
51
            curl_setopt($ch, CURLOPT_TIMEOUT, 240);
52
            $result = curl_exec($ch);
53
            curl_close($ch);
54
55
            $result = json_decode($result);
56
57
            if ($result->status === 'success') {
58
                return $this->response(202, $result, null, $this->className);
59
            } else {
60
                return $this->response(500, $result->message, null, $this->className);
61
            }
62
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
63
64
        }
65
    }
66
67
    /**
68
     * This function gets the account balance.
69
     *
70
     */
71 View Code Duplication
    public function balance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        try {
74
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
75
            $query = [
76
                'api' => $conf['api'],
77
            ];
78
79
            $url = $conf['url'] . '/account?' . http_build_query($query);
80
81
            $ch = curl_init();
82
            curl_setopt($ch,CURLOPT_URL,$url);
83
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84
            curl_setopt($ch, CURLOPT_TIMEOUT, 240);
85
            $result = curl_exec($ch);
86
            curl_close($ch);
87
88
            $result = json_decode($result);
89
90
            if ($result->status === 'success') {
91
                return $this->response(200, $result, null, $this->className);
92
            } else {
93
                return $this->response(500, $result->message, null, $this->className);
94
            }
95
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
96
97
        }
98
    }
99
}