Completed
Push — master ( e6f297...cd788c )
by Kazi Mainuddin
12s
created

Kavenegar   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 15
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 15 15 2
A getSmsResponse() 0 14 2

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 Tzsk\Sms\Drivers;
3
4
use Kavenegar\KavenegarApi;
5
use Tzsk\Sms\Abstracts\Driver;
6
7
class Kavenegar extends Driver
8
{
9
    /**
10
     * Kavenegar Settings.
11
     *
12
     * @var null|object
13
     */
14
    protected $settings = null;
15
16
    /**
17
     * Kavenegar Client.
18
     *
19
     * @var null|KavenegarApi
20
     */
21
    protected $client = null;
22
23
    /**
24
     * Construct the class with the relevant settings.
25
     *
26
     * SendSmsInterface constructor.
27
     * @param $settings object
28
     */
29
    public function __construct($settings)
30
    {
31
        $this->settings = (object) $settings;
32
        $this->client = new KavenegarApi($this->settings->apiKey);
33
    }
34
35
    /**
36
     * Send text message and return response.
37
     *
38
     * @return object
39
     */
40 View Code Duplication
    public function send()
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...
41
    {
42
        $response = [];
43
        foreach ($this->recipients as $recipient) {
44
            $sms = $this->client->Send(
45
                $this->settings->from,
46
                $recipient,
47
                $this->body
48
            );
49
            $response[$recipient]['data'] = $this->getSmsResponse($sms[0]);
50
            $response[$recipient]['status'] = true;
51
        }
52
53
        return (object) $response;
54
    }
55
56
    /**
57
     * Get the Kavenegar Response.
58
     *
59
     * @param $sms
60
     * @return object
61
     */
62
    protected function getSmsResponse($sms)
63
    {
64
        $attributes = [
65
            'messageid', 'message', 'status', 'statustext',
66
            'sender', 'receptor', 'date', 'cost',
67
        ];
68
69
        $res = [];
70
        foreach ($attributes as $attribute) {
71
            $res[$attribute] = $sms->$attribute;
72
        }
73
74
        return (object) $res;
75
    }
76
}
77