Completed
Push — master ( fcb419...62d24f )
by Kazi Mainuddin
03:08
created

SmsManager::with()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
namespace Tzsk\Sms;
3
4
5
class SmsManager
6
{
7
    /**
8
     * Sms Configuration.
9
     *
10
     * @var null|object
11
     */
12
    protected $config = null;
13
14
    /**
15
     * Sms Driver Settings.
16
     *
17
     * @var null|object
18
     */
19
    protected $settings = null;
20
21
    /**
22
     * Sms Driver Name.
23
     *
24
     * @var null|string
25
     */
26
    protected $driver = null;
27
28
    /**
29
     * SmsManager constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->config = config('sms');
34
        $this->driver = $this->config['default'];
35
        $this->settings = $this->config['drivers'][$this->driver];
36
    }
37
38
    /**
39
     * Change the driver on the fly.
40
     *
41
     * @param $driver
42
     * @return $this
43
     */
44
    public function with($driver) {
45
        $this->driver = $driver;
46
        $this->settings = $this->config['drivers'][$this->driver];
47
48
        return $this;
49
    }
50
51
    /**
52
     * Send message.
53
     *
54
     * @param $message
55
     * @param $callback
56
     * @return mixed
57
     * @throws \Exception
58
     */
59
    public function send($message, $callback) {
60
        $this->validateParams();
61
62
        $class = $this->config['map'][$this->driver];
63
        $object = new $class($this->settings);
64
        $object->message($message);
65
        call_user_func($callback, $object);
66
67
        return $object->send();
68
    }
69
70
    /**
71
     * Validate Parameters before sending.
72
     */
73
    protected function validateParams()
74
    {
75
        if (!$this->driver) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->driver of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
76
            throw new \Exception("Driver not selected or default driver does not exist.");
77
        }
78
        if (empty($this->config['drivers'][$this->driver]) OR empty($this->config['map'][$this->driver])) {
79
            throw new \Exception("Driver not found in config file. Try updating the package.");
80
        }
81
82
        if (!class_exists($this->config['map'][$this->driver])) {
83
            throw new \Exception("Driver source not found. Please update the package.");
84
        }
85
    }
86
87
}
88