Completed
Pull Request — master (#13)
by
unknown
04:09
created

SmsManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A withDriver() 0 7 1
A send() 0 11 1
A validateParams() 0 13 5
1
<?php
2
namespace Tzsk\Sms;
3
4
class SmsManager
5
{
6
    /**
7
     * Sms Configuration.
8
     *
9
     * @var null|object
10
     */
11
    protected $config = null;
12
13
    /**
14
     * Sms Driver Settings.
15
     *
16
     * @var null|object
17
     */
18
    protected $settings = null;
19
20
    /**
21
     * Sms Driver Name.
22
     *
23
     * @var null|string
24
     */
25
    protected $driver = null;
26
27
    /**
28
     * SmsManager constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->config = config('sms');
33
        $this->withDriver($this->config['default']);
34
    }
35
36
    /**
37
     * Change the driver on the fly.
38
     *
39
     * @param $driver
40
     * @return $this
41
     */
42
    public function withDriver($driver)
43
    {
44
        $this->driver = $driver;
45
        $this->settings = $this->config['drivers'][$this->driver];
46
47
        return $this;
48
    }
49
50
    /**
51
     * Send message.
52
     *
53
     * @param $message
54
     * @param $callback
55
     * @return mixed
56
     */
57
    public function send($message, $callback)
58
    {
59
        $this->validateParams();
60
61
        $class = $this->config['map'][$this->driver];
62
        $object = new $class($this->settings);
63
        $object->message($message);
64
        call_user_func($callback, $object);
65
66
        return $object->send();
67
    }
68
69
    /**
70
     * Validate Parameters before sending.
71
     *
72
     * @throws \Exception
73
     */
74
    protected function validateParams()
75
    {
76
        if (empty($this->driver)) {
77
            throw new \Exception("Driver not selected or default driver does not exist.");
78
        }
79
        if (empty($this->config['drivers'][$this->driver]) or empty($this->config['map'][$this->driver])) {
80
            throw new \Exception("Driver not found in config file. Try updating the package.");
81
        }
82
83
        if (!class_exists($this->config['map'][$this->driver])) {
84
            throw new \Exception("Driver source not found. Please update the package.");
85
        }
86
    }
87
}
88