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

ShortMessageCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 67
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 12 2
A toXml() 0 10 2
A toArray() 0 16 2
1
<?php
2
3
namespace Erdemkeren\JetSms;
4
5
/**
6
 * Class ShortMessageCollection.
7
 */
8
class ShortMessageCollection
9
{
10
    /**
11
     * The items of the collection.
12
     *
13
     * @var array
14
     */
15
    protected $items = [];
16
17
    /**
18
     * Push a new short message to the given collection.
19
     *
20
     * @param ShortMessage $shortMessage
21
     *
22
     * @return self
23
     */
24 3
    public function push(ShortMessage $shortMessage)
25
    {
26 3
        if ($shortMessage->hasManyReceivers()) {
27 1
            throw new \LogicException(
28
                "Expected one receiver per short message, got many."
29 1
            );
30
        }
31
32 3
        $this->items[] = $shortMessage;
33
34 3
        return $this;
35
    }
36
37
    /**
38
     * Get the xml representation of the items.
39
     *
40
     * @return string
41
     */
42 1
    public function toXml()
43
    {
44 1
        $messages = '';
45
        /** @var ShortMessage $shortMessage */
46 1
        foreach ($this->items as $shortMessage) {
47 1
            $messages .= $shortMessage->toMultipleMessagesXml();
48 1
        }
49
50 1
        return $messages;
51
    }
52
53
    /**
54
     * Get the array presentation of the items.
55
     *
56
     * @return array
57
     */
58 1
    public function toArray()
59
    {
60 1
        $messages = [];
61 1
        $receivers = [];
62
63
        /** @var ShortMessage $shortMessage */
64 1
        foreach ($this->items as $shortMessage) {
65 1
            $messages[] = $shortMessage->body();
66 1
            $receivers[] = $shortMessage->receiversString();
67 1
        }
68
69
        return [
70 1
            'Msisdns'        => implode('|', $receivers),
71 1
            'Messages'       => implode('|', $messages),
72 1
        ];
73
    }
74
}
75