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

Melipayamak   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A asFlash() 0 6 1
A send() 16 16 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 Melipayamak\MelipayamakApi;
5
use Tzsk\Sms\Abstracts\Driver;
6
7
class Melipayamak extends Driver
8
{
9
    /**
10
     * Melipayamak Settings.
11
     *
12
     * @var null|object
13
     */
14
    protected $settings = null;
15
16
    /**
17
     * Melipayamak Client.
18
     *
19
     * @var null|MelipayamakApi
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 MelipayamakApi($this->settings->username, $this->settings->password);
33
    }
34
35
    /**
36
     * Determine if the sms must be a flash message or not.
37
     *
38
     * @param bool $flash
39
     * @return $this
40
     */
41
    public function asFlash($flash=true)
42
    {
43
        $this->settings->flash = $flash;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Send text message and return response.
50
     *
51
     * @return object
52
     */
53 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...
54
    {
55
        $response = [];
56
        foreach ($this->recipients as $recipient) {
57
            $sms = $this->client->sms()->send(
58
                $recipient,
59
                $this->settings->from,
60
                $this->body,
61
                $this->settings->flash
62
            );
63
            $response[$recipient]['data'] = $this->getSmsResponse($sms);
64
            $response[$recipient]['status'] = true;
65
        }
66
67
        return (object) $response;
68
    }
69
70
    /**
71
     * Get the Melipayamak Response.
72
     *
73
     * @param $sms
74
     * @return object
75
     */
76
    protected function getSmsResponse($sms)
77
    {
78
        $sms = json_decode($sms, true);
79
        $attributes = [
80
            'Value', 'RetStatus', 'StrRetStatus'
81
        ];
82
83
        $res = [];
84
        foreach ($attributes as $attribute) {
85
            $res[$attribute] = $sms[$attribute];
86
        }
87
88
        return (object) $res;
89
    }
90
}
91