Completed
Push — master ( 1a07cb...6b9ee1 )
by Kazi Mainuddin
01:45 queued 10s
created

SmsManager::validateDriver()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Tzsk\Sms;
4
5
use Exception;
6
use ReflectionClass;
7
8
class SmsManager
9
{
10
    /**
11
     * Sms Configuration.
12
     *
13
     * @var array
14
     */
15
    protected $config;
16
17
    /**
18
     * Sms Driver Settings.
19
     *
20
     * @var array
21
     */
22
    protected $settings;
23
24
    /**
25
     * Sms Driver Name.
26
     *
27
     * @var string
28
     */
29
    protected $driver;
30
31
    /**
32
     * @var SmsBuilder
33
     */
34
    protected $builder;
35
36
    /**
37
     * SmsManager constructor.
38
     *
39
     * @param array $config
40
     */
41
    public function __construct($config)
42
    {
43
        $this->config = $config;
44
        $this->setBuilder(new SmsBuilder());
45
        $this->via($this->config['default']);
46
    }
47
48
    /**
49
     * @param string|array $recipients
50
     * @return self
51
     */
52
    public function to($recipients)
53
    {
54
        $this->builder->to($recipients);
55
56
        return $this;
57
    }
58
59
    /**
60
     * Change the driver on the fly.
61
     *
62
     * @param $driver
63
     * @return $this
64
     * @throws \Exception
65
     */
66 View Code Duplication
    public function via($driver)
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...
67
    {
68
        $this->driver = $driver;
69
        $this->validateDriver();
70
        $this->builder->via($driver);
71
        $this->settings = $this->config['drivers'][$driver];
72
73
        return $this;
74
    }
75
76
    /**
77
     * Send message.
78
     *
79
     * @param $message
80
     * @param $callback
81
     * @return mixed
82
     * @throws \Exception
83
     */
84
    public function send($message, $callback = null)
85
    {
86
        if ($message instanceof SmsBuilder) {
87
            return $this->setBuilder($message)->dispatch();
88
        }
89
90
        $this->builder->send($message);
91
        if (! $callback) {
92
            return $this;
93
        }
94
95
        $driver = $this->getDriverInstance();
96
        $driver->message($message);
97
        call_user_func($callback, $driver);
98
99
        return $driver->send();
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105
    public function dispatch()
106
    {
107
        $this->driver = $this->builder->getDriver() ?: $this->driver;
108
        if (empty($this->driver)) {
109
            $this->via($this->config['default']);
110
        }
111
        $driver = $this->getDriverInstance();
112
        $driver->message($this->builder->getBody());
113
        $driver->to($this->builder->getRecipients());
114
115
        return $driver->send();
116
    }
117
118
    /**
119
     * @param SmsBuilder $builder
120
     * @return self
121
     */
122
    protected function setBuilder(SmsBuilder $builder)
123
    {
124
        $this->builder = $builder;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Generate driver instance.
131
     *
132
     * @return mixed
133
     */
134 View Code Duplication
    protected function getDriverInstance()
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...
135
    {
136
        $this->validateDriver();
137
        $class = $this->config['map'][$this->driver];
138
139
        return new $class($this->settings);
140
    }
141
142
    /**
143
     * Validate Parameters before sending.
144
     *
145
     * @throws \Exception
146
     */
147
    protected function validateDriver()
148
    {
149
        $conditions = [
150
            'Driver not selected or default driver does not exist.' => empty($this->driver),
151
            'Driver not found in config file. Try updating the package.' => empty($this->config['drivers'][$this->driver]) || empty($this->config['map'][$this->driver]),
152
            'Driver source not found. Please update the package.' => ! class_exists($this->config['map'][$this->driver]),
153
            'Driver must be an instance of Contracts\DriverInterface.' => ! (new ReflectionClass($this->config['map'][$this->driver]))->implementsInterface(Contracts\DriverInterface::class),
154
        ];
155
156
        foreach ($conditions as $ex => $condition) {
157
            throw_if($condition, new Exception($ex));
158
        }
159
    }
160
}
161