Completed
Push — master ( b898f3...cca52f )
by Hilmi Erdem
01:28
created

JetSmsService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 136
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A sendShortMessage() 0 16 3
B sendShortMessages() 0 23 4
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 ShortMessageFactoryInterface
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
    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
        $this->client = $jetSmsClient;
83
        $this->factory = $shortMessageFactory;
84
        $this->collectionFactory = $shortMessageCollectionFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $shortMessageCollectionFactory of type object<Erdemkeren\JetSms...ectionFactoryInterface> is incompatible with the declared type object<Erdemkeren\JetSms...essageFactoryInterface> of property $collectionFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
        $this->beforeSingleShortMessageCallback = $beforeSingleShortMessageCallback;
86
        $this->afterSingleShortMessageCallback = $afterSingleShortMessageCallback;
87
        $this->beforeMultipleShortMessageCallback = $beforeMultipleShortMessageCallback;
88
        $this->afterMultipleShortMessageCallback = $afterMultipleShortMessageCallback;
89
    }
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
    public function sendShortMessage($receivers, $body)
100
    {
101
        $shortMessage = $this->factory->create($receivers, $body);
102
103
        if(is_callable($this->beforeSingleShortMessageCallback)) {
104
            ($this->beforeSingleShortMessageCallback)($shortMessage);
105
        }
106
107
        $response = $this->client->sendShortMessage($shortMessage);
108
109
        if(is_callable($this->afterSingleShortMessageCallback)) {
110
            ($this->afterSingleShortMessageCallback)($response, $shortMessage);
111
        }
112
113
        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
    public function sendShortMessages(array $messages)
124
    {
125
        $collection = $this->collectionFactory->create();
0 ignored issues
show
Bug introduced by
The call to create() misses some required arguments starting with $receivers.
Loading history...
126
127
        if(is_callable($this->beforeMultipleShortMessageCallback)) {
128
            ($this->beforeMultipleShortMessageCallback)($collection);
129
        }
130
131
        foreach ($messages as $message) {
132
            $collection->push($this->factory->create(
0 ignored issues
show
Bug introduced by
The method push() does not seem to exist on object<Erdemkeren\JetSms\ShortMessage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
                $message['recipient'],
134
                $message['message']
135
            ));
136
        }
137
138
        $response = $this->client->sendShortMessages($collection);
0 ignored issues
show
Documentation introduced by
$collection is of type object<Erdemkeren\JetSms\ShortMessage>, but the function expects a object<Erdemkeren\JetSms\ShortMessageCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
140
        if(is_callable($this->afterMultipleShortMessageCallback)) {
141
            ($this->afterMultipleShortMessageCallback)($response, $collection);
142
        }
143
144
        return $response;
145
    }
146
}
147