AbstractMessageBuilderModel::addThreadData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the MilioooMessageBundle package.
5
 *
6
 * (c) Michiel boeckaert <[email protected]>
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Miliooo\Messaging\Builder\Model;
12
13
use Miliooo\Messaging\Form\FormModel\NewMessageInterface;
14
use Miliooo\Messaging\User\ParticipantInterface;
15
use Miliooo\Messaging\Model\MessageMetaInterface;
16
use Miliooo\Messaging\ValueObjects\ReadStatus;
17
18
/**
19
 * Description of AbstractMessageBuilderModel
20
 *
21
 * @author Michiel Boeckaert <[email protected]>
22
 */
23
abstract class AbstractMessageBuilderModel
24
{
25
    const SENDER = 'sender';
26
    const ALL = 'all';
27
    const RECIPIENTS = 'recipients';
28
29
    private static $group = [
30
        'sender' => self::SENDER,
31
        'all' => self::ALL,
32
        'recipients' => self::RECIPIENTS
33
    ];
34
35
    /**
36
     * @var NewMessageInterface
37
     */
38
    protected $messageModel;
39
40
    /**
41
     * The array where we'll save all the data to
42
     *
43
     * @var array
44
     */
45
    protected $data = [];
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param NewMessageInterface $messageModel
51
     */
52
    public function __construct(NewMessageInterface $messageModel)
53
    {
54
        $this->messageModel = $messageModel;
55
        $this->processDefaultMessageData();
56
    }
57
58
    /**
59
     * This processes the default message data.
60
     *
61
     * This method is called in the constructor and populates the default new message data.
62
     * It also calls a method process extra which classes who extend this class have to implement.
63
     *
64
     * There we process some more default data. See the threadbuildermodel for an example.
65
     *
66
     * If you want to overwrite this data (not recommended!!) you can call the same method with the same key.
67
     * This will overwrite that data in the array with your given value.
68
     */
69
    protected function processDefaultMessageData()
70
    {
71
        $this->addMessageData('body', $this->messageModel->getBody());
72
        $this->addMessageData('createdAt', $this->messageModel->getCreatedAt());
73
        $this->addMessageData('sender', $this->messageModel->getSender());
74
75
        $this->addMessageMeta(self::SENDER, 'readStatus', new ReadStatus(MessageMetaInterface::READ_STATUS_READ));
76
        $this->addMessageMeta(self::RECIPIENTS, 'readStatus', new ReadStatus(MessageMetaInterface::READ_STATUS_NEVER_READ));
77
78
        $this->addThreadMeta(self::SENDER, 'lastParticipantMessageDate', $this->messageModel->getCreatedAt());
79
        $this->addThreadMeta(self::RECIPIENTS, 'lastMessageDate', $this->messageModel->getCreatedAt());
80
81
        $this->processExtra();
82
    }
83
84
    /**
85
     * Gets the sender of the message
86
     *
87
     * @return ParticipantInterface
88
     */
89
    public function getSender()
90
    {
91
        return $this->messageModel->getSender();
92
    }
93
94
    /**
95
     * Add message data to your message class.
96
     *
97
     * @param string $key   The name of your classes attribute, example ip => builder will call setIp
98
     * @param mixed  $value The value of the attribute you want to set
99
     */
100
    public function addMessageData($key, $value)
101
    {
102
        $this->addData('message', $key, $value);
103
    }
104
105
    /**
106
     * Returns the message data.
107
     *
108
     * The key is the attribute name and the value is the value.
109
     * Example data['subject' => 'The subject of the message, 'createdAt' => \Datetime object]
110
     *
111
     * @return array An array with message data needed to populate the message class.
112
     */
113
    public function getMessageData()
114
    {
115
        return $this->getData('message');
116
    }
117
118
    /**
119
     * Add thread data to your thread class.
120
     *
121
     * @param string $key   The name of your thread class attribute
122
     * @param mixed  $value The value for your attribute
123
     */
124
    public function addThreadData($key, $value)
125
    {
126
        $this->addData('thread', $key, $value);
127
    }
128
129
    /**
130
     * Gets the data for the thread class.
131
     *
132
     * This function is used by the builders to populate the thread object.
133
     *
134
     * @return array||null An array where the keys are the names of the attributes and the values the attribute values
0 ignored issues
show
Documentation introduced by
The doc-type array||null could not be parsed: Unknown type name "|" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
     */
136
    public function getThreadData()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
137
    {
138
        return $this->getData('thread');
139
    }
140
141
    /**
142
     * Adds message meta for a given participant group to your message meta class.
143
     *
144
     * @param string $participant One of the class constants SENDER ALL RECIPIENTS
145
     * @param string $key         The name of your message meta class attribute you want to add data for.
146
     * @param mixed  $value       The value for your attribute
147
     */
148
    public function addMessageMeta($participant, $key, $value)
149
    {
150
        $this->addMeta('messageMeta', $participant, $key, $value);
151
    }
152
153
    /**
154
     * Returns the message meta for the given group of participants.
155
     *
156
     * @param string $participant One of the class constants SENDER ALL RECIPIENTS
157
     *
158
     * @return array||null An array where the keys are the names of the attributes and the values the attribute values
0 ignored issues
show
Documentation introduced by
The doc-type array||null could not be parsed: Unknown type name "|" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
159
     */
160
    public function getMessageMeta($participant)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
161
    {
162
        return $this->getMeta('messageMeta', $participant);
163
    }
164
165
    /**
166
     * Adds thread meta for a given participant group to the thread meta class.
167
     *
168
     * @param string $participant One of the class constants SENDER ALL RECIPIENTS
169
     * @param string $key         The name of your thread meta class attribute
170
     * @param mixed  $value       The value of your thread meta class attribute
171
     */
172
    public function addThreadMeta($participant, $key, $value)
173
    {
174
        $this->addMeta('threadMeta', $participant, $key, $value);
175
    }
176
177
    /**
178
     * Returns the thread meta for the given group of participants.
179
     *
180
     * @param string $participant One of the class constants SENDER ALL RECIPIENTS
181
     *
182
     * @return array||null An array where the keys are the names of the attributes and the values the attribute values
0 ignored issues
show
Documentation introduced by
The doc-type array||null could not be parsed: Unknown type name "|" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
183
     */
184
    public function getThreadMeta($participant)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
185
    {
186
        return $this->getMeta('threadMeta', $participant);
187
    }
188
189
    /**
190
     * Returns the form model used to set the default data.
191
     *
192
     * If you added more data to the form model you can ask for the model and update the data in the processors.
193
     */
194
    public function getFormModel()
195
    {
196
        return $this->messageModel;
197
    }
198
199
    /**
200
     * Helper function to populate the data array.
201
     *
202
     * @param string $name        Name of the meta one of threadMeta, messageMeta
203
     * @param string $participant One of the class constants SENDER ALL RECIPIENTS
204
     * @param string $key         The name of the class attribute
205
     * @param mixed  $value       The value of the class attribute
206
     */
207
    protected function addMeta($name, $participant, $key, $value)
208
    {
209
        $this->data[$name][self::$group[$participant]][$key] = $value;
210
    }
211
212
    /**
213
     * Helper function to get meta.
214
     *
215
     * @param string $name        Name of the meta one of threadMeta, messageMeta
216
     * @param string $participant One of the class constants SENDER ALL RECIPIENTS
217
     *
218
     * @return array||null An array where the keys are the names of the attributes and the values the attribute values
0 ignored issues
show
Documentation introduced by
The doc-type array||null could not be parsed: Unknown type name "|" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
219
     */
220
    protected function getMeta($name, $participant)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
221
    {
222
        return isset($this->data[$name][self::$group[$participant]]) ?
223
            $this->data[$name][self::$group[$participant]] : null;
224
    }
225
226
    /**
227
     * Helper function to add data to the array.
228
     *
229
     * @param string $name  One of message, thread
230
     * @param string $key   Name of the attribute for the given class
231
     * @param mixed  $value Value for that attribute
232
     */
233
    protected function addData($name, $key, $value)
234
    {
235
        $this->data[$name][$key] = $value;
236
    }
237
238
    /**
239
     * Helper function to get data from the array.
240
     *
241
     * @param string $name One of message, thread
242
     *
243
     * @return array||null An array where the keys are the names of the attributes and the values the attribute values
0 ignored issues
show
Documentation introduced by
The doc-type array||null could not be parsed: Unknown type name "|" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
244
     */
245
    protected function getData($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
246
    {
247
        return isset($this->data[$name]) ? $this->data[$name] : null;
248
    }
249
250
    /**
251
     * Processes more default data specific to a new thread or a new reply.     *
252
     */
253
    abstract protected function processExtra();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
254
}
255