AbstractMessage   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 123
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1
ccs 29
cts 29
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addSegment() 0 4 1
A getSegments() 0 4 1
A setDialogId() 0 4 1
A getDialogId() 0 4 1
A setMessageNumber() 0 4 1
A getMessageNumber() 0 4 1
A toString() 0 10 2
A __toString() 0 4 1
A buildMessageHeader() 0 9 2
1
<?php
2
3
namespace Fhp\Message;
4
5
use Fhp\Segment\HNHBK;
6
use Fhp\Segment\SegmentInterface;
7
8
/**
9
 * Class AbstractMessage
10
 * @package Fhp\Message
11
 */
12
class AbstractMessage
13
{
14
    const MSG_HEADER_SEGMENT = 'HNHBK';
15
    const MSG_HEADER_VERSION = 3;
16
    const MSG_HEADER_SEG_NUMBER = 1;
17
    const MSG_HBCI_VERSION = '300';
18
19
    const OPT_PINTAN_MECH = 'pintan_mechanism';
20
21
    /**
22
     * @var array
23
     */
24
    protected $segments = array();
25
26
    /**
27
     * @var int
28
     */
29
    protected $dialogId = 0;
30
31
    /**
32
     * @var int
33
     */
34
    protected $messageNumber = 1;
35
36
    /**
37
     * Adds a segment to the message.
38
     *
39
     * @param SegmentInterface $segment
40
     */
41 4
    protected function addSegment(SegmentInterface $segment)
42
    {
43 4
        $this->segments[] = $segment;
44 4
    }
45
46
    /**
47
     * Gets all segments of a message.
48
     *
49
     * @return array
50
     */
51 1
    public function getSegments()
52
    {
53 1
        return $this->segments;
54
    }
55
56
    /**
57
     * Sets the dialog ID.
58
     *
59
     * @param $dialogId
60
     */
61 1
    public function setDialogId($dialogId)
62
    {
63 1
        $this->dialogId = $dialogId;
64 1
    }
65
66
    /**
67
     * Gets the dialog ID.
68
     *
69
     * @return int
70
     */
71 1
    public function getDialogId()
72
    {
73 1
        return $this->dialogId;
74
    }
75
76
    /**
77
     * Sets the message number.
78
     *
79
     * @param int $number
80
     */
81 1
    public function setMessageNumber($number)
82
    {
83 1
        $this->messageNumber = (int) $number;
84 1
    }
85
86
    /**
87
     * Gets the message number.
88
     *
89
     * @return int
90
     */
91 1
    public function getMessageNumber()
92
    {
93 1
        return $this->messageNumber;
94
    }
95
96
    /**
97
     * Transform message to HBCI string.
98
     *
99
     * @return string
100
     */
101 2
    public function toString()
102
    {
103 2
        $string = (string) $this->buildMessageHeader();
104
105 2
        foreach ($this->segments as $segment) {
106 2
            $string .= (string) $segment;
107 2
        }
108
109 2
        return $string;
110
    }
111
112
    /**
113
     * @return string
114
     */
115 2
    public function __toString()
116
    {
117 2
        return $this->toString();
118
    }
119
120
    /**
121
     * Builds the message header.
122
     *
123
     * @return HNHBK
124
     */
125 2
    protected function buildMessageHeader()
126
    {
127 2
        $len = 0;
128 2
        foreach ($this->segments as $segment) {
129 2
            $len += strlen($segment);
130 2
        }
131
132 2
        return new HNHBK($len, $this->dialogId, $this->messageNumber);
133
    }
134
}
135