NewThreadBuilder::buildThreadMetaForSender()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Message;
12
13
use Miliooo\Messaging\Builder\Model\ThreadBuilderModel;
14
use Miliooo\Messaging\Model\ThreadInterface;
15
use Miliooo\Messaging\User\ParticipantInterface;
16
use Miliooo\Messaging\Model\ThreadMetaInterface;
17
18
/**
19
 * Builder for new threads
20
 *
21
 * @author Michiel Boeckaert <[email protected]>
22
 */
23
class NewThreadBuilder extends AbstractMessageBuilder
24
{
25
    protected $builderModel;
26
    protected $sender;
27
    protected $recipients = [];
28
29
    /**
30
     * Builds a new thread object with one new message
31
     *
32
     * @param ThreadBuilderModel $builderModel
33
     *
34
     * @return ThreadInterface
35
     */
36
    public function build(ThreadBuilderModel $builderModel)
37
    {
38
        $this->builderModel = $builderModel;
39
        $this->sender = $this->builderModel->getSender();
40
        $this->recipients = $this->builderModel->getRecipients();
41
42
        $thread = $this->createThread();
43
        $this->setThreadData($thread);
44
        $this->createThreadMetaForNewThread($thread);
45
        $this->buildNewMessage($thread);
46
47
        return $thread;
48
    }
49
50
    /**
51
     * We update the thread with the thread data from the builder
52
     *
53
     * @param ThreadInterface $thread
54
     */
55
    protected function setThreadData(ThreadInterface $thread)
56
    {
57
        $this->processBuilderModel('getThreadData', null, $thread);
58
    }
59
60
    /**
61
     * Creates new thread meta for a single participant
62
     *
63
     * @param ThreadInterface      $thread      The thread we are building
64
     * @param ParticipantInterface $participant The participant whom we create thread meta for
65
     *
66
     * @return ThreadMetaInterface
67
     */
68
    private function createThreadMetaForParticipant(ThreadInterface $thread, ParticipantInterface $participant)
69
    {
70
        //creates an empty thread meta object
71
        $threadMeta = $this->createThreadMeta();
72
73
        //adds the participant to the threadmeta
74
        $threadMeta->setParticipant($participant);
75
76
        //adds the thread to the thread meta
77
        $threadMeta->setThread($thread);
78
79
        //adds the thread meta to the thread
80
        $thread->addThreadMeta($threadMeta);
81
82
        return $threadMeta;
83
    }
84
85
    /**
86
     * Creates a new thread meta object from the config (user specified) thread meta class
87
     *
88
     * @return ThreadMetaInterface
89
     */
90
    private function createThreadMeta()
91
    {
92
        return new $this->threadMetaClass();
93
    }
94
95
    /**
96
     * Creates thread meta for a new thread.
97
     *
98
     * We need thread meta for each participant
99
     *
100
     * @param ThreadInterface $thread The thread we create thread meta for
101
     */
102
    private function createThreadMetaForNewThread(ThreadInterface $thread)
103
    {
104
        $this->buildThreadMetaForSender($thread);
105
106
        foreach ($this->recipients as $recipient) {
107
            $this->buildThreadMetaForRecipient($thread, $recipient);
108
        }
109
    }
110
111
    /**
112
     * Builds the thread meta for the sender
113
     *
114
     * @param ThreadInterface $thread
115
     */
116
    private function buildThreadMetaForSender(ThreadInterface $thread)
117
    {
118
        $threadMeta = $this->createThreadMetaForParticipant($thread, $this->sender);
119
        $this->updateThreadMetaForSender($threadMeta);
120
    }
121
122
    /**
123
     * Builds the thread meta for the recipient
124
     *
125
     * @param ThreadInterface      $thread
126
     * @param ParticipantInterface $recipient
127
     */
128
    private function buildThreadMetaForRecipient(ThreadInterface $thread, ParticipantInterface $recipient)
129
    {
130
        $threadMeta = $this->createThreadMetaForParticipant($thread, $recipient);
131
        $this->updateThreadMetaForRecipient($threadMeta);
132
    }
133
134
    /**
135
     * Creates a new thread object from the config (user specified) thread class
136
     *
137
     * @return ThreadInterface
138
     */
139
    private function createThread()
140
    {
141
        return new $this->threadClass();
142
    }
143
}
144