Completed
Push — master ( 5c498e...edddde )
by Hilmi Erdem
01:29
created

JetSmsService::sendShortMessage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Erdemkeren\JetSms;
4
5
use Erdemkeren\JetSms\Http\Clients\JetSmsClientInterface;
6
use Erdemkeren\JetSms\Http\Responses\JetSmsResponseInterface;
7
8
/**
9
 * Class JetSmsService.
10
 */
11
final class JetSmsService
12
{
13
    /**
14
     * The jet sms client implementation.
15
     *
16
     * @var JetSmsClientInterface
17
     */
18
    private $client;
19
20
    /**
21
     * The short message factory implementation.
22
     *
23
     * @var ShortMessageFactoryInterface
24
     */
25
    private $factory;
26
27
    /**
28
     * The short message collection factory implementation.
29
     *
30
     * @var ShortMessageCollectionFactoryInterface
31
     */
32
    private $collectionFactory;
33
34
    /**
35
     * The before callback which will be called before sending single messages.
36
     *
37
     * @var callable|null
38
     */
39
    private $beforeSingleShortMessageCallback;
40
41
    /**
42
     * The after callback which will be called before sending single messages.
43
     *
44
     * @var callable|null
45
     */
46
    private $afterSingleShortMessageCallback;
47
48
    /**
49
     * The before callback which will be called before sending multiple messages.
50
     *
51
     * @var callable|null
52
     */
53
    private $beforeMultipleShortMessageCallback;
54
55
    /**
56
     * The after callback which will be called after sending multiple messages.
57
     *
58
     * @var callable|null
59
     */
60
    private $afterMultipleShortMessageCallback;
61
62
    /**
63
     * JetSmsService constructor.
64
     *
65
     * @param  JetSmsClientInterface                  $jetSmsClient
66
     * @param  ShortMessageFactoryInterface           $shortMessageFactory
67
     * @param  ShortMessageCollectionFactoryInterface $shortMessageCollectionFactory
68
     * @param  callable|null                          $beforeSingleShortMessageCallback
69
     * @param  callable|null                          $afterSingleShortMessageCallback
70
     * @param  callable|null                          $beforeMultipleShortMessageCallback
71
     * @param  callable|null                          $afterMultipleShortMessageCallback
72
     */
73 4
    public function __construct(
74
        JetSmsClientInterface $jetSmsClient,
75
        ShortMessageFactoryInterface $shortMessageFactory,
76
        ShortMessageCollectionFactoryInterface $shortMessageCollectionFactory,
77
        $beforeSingleShortMessageCallback = null,
78
        $afterSingleShortMessageCallback = null,
79
        $beforeMultipleShortMessageCallback = null,
80
        $afterMultipleShortMessageCallback = null
81
    ) {
82 4
        $this->client = $jetSmsClient;
83 4
        $this->factory = $shortMessageFactory;
84 4
        $this->collectionFactory = $shortMessageCollectionFactory;
85 4
        $this->beforeSingleShortMessageCallback = $beforeSingleShortMessageCallback;
86 4
        $this->afterSingleShortMessageCallback = $afterSingleShortMessageCallback;
87 4
        $this->beforeMultipleShortMessageCallback = $beforeMultipleShortMessageCallback;
88 4
        $this->afterMultipleShortMessageCallback = $afterMultipleShortMessageCallback;
89 4
    }
90
91
    /**
92
     * Send the given body to the given receivers.
93
     *
94
     * @param  string       $body      The body of the short message.
95
     * @param  array|string $receivers The receiver(s) of the message.
96
     *
97
     * @return JetSmsResponseInterface The parsed JetSms response object.
98
     */
99 3
    public function sendShortMessage($receivers, $body)
100
    {
101 3
        $shortMessage = $this->factory->create($receivers, $body);
102
103 3
        if (is_callable($this->beforeSingleShortMessageCallback)) {
104 1
            call_user_func_array($this->beforeSingleShortMessageCallback, [$shortMessage]);
105 1
        }
106
107 3
        $response = $this->client->sendShortMessage($shortMessage);
108
109 3
        if (is_callable($this->afterSingleShortMessageCallback)) {
110 1
            call_user_func_array($this->afterSingleShortMessageCallback, [$response, $shortMessage]);
111 1
        }
112
113 3
        return $response;
114
    }
115
116
    /**
117
     * Send the given short messages.
118
     *
119
     * @param  array $messages         An array containing short message arrays.
120
     *
121
     * @return JetSmsResponseInterface The parsed JetSms response object.
122
     */
123 2
    public function sendShortMessages(array $messages)
124
    {
125 2
        $collection = $this->collectionFactory->create();
126
127 2
        if (is_callable($this->beforeMultipleShortMessageCallback)) {
128 1
            call_user_func_array($this->beforeMultipleShortMessageCallback, [$collection]);
129 1
        }
130
131 2
        foreach ($messages as $message) {
132 2
            $collection->push($this->factory->create(
133 2
                $message['recipient'],
134 2
                $message['message']
135 2
            ));
136 2
        }
137
138 2
        $response = $this->client->sendShortMessages($collection);
139
140 2
        if (is_callable($this->afterMultipleShortMessageCallback)) {
141 1
            call_user_func_array($this->afterMultipleShortMessageCallback, [$response, $collection]);
142 1
        }
143
144 2
        return $response;
145
    }
146
}
147