Passed
Push — master ( 5600bf...8e982a )
by Gabriel
05:21
created

RecipientTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 25
c 1
b 0
f 0
dl 0
loc 99
ccs 10
cts 30
cp 0.3333
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecipient() 0 3 1
A getRecipientModelFromEvent() 0 8 2
A generateNotifiablesForEvent() 0 10 2
A generateRecipientGetterMethod() 0 3 1
A getRecipientManager() 0 6 2
A isActive() 0 3 1
A generateRecipientManager() 0 3 1
A getNotificationMessage() 0 8 1
1
<?php
2
3
namespace ByTIC\Notifier\Models\Recipients;
4
5
use ByTIC\Common\Records\Traits\HasTypes\RecordTrait;
6
use ByTIC\Notifier\Exceptions\NotificationModelNotFoundException;
7
use ByTIC\Notifier\Exceptions\NotificationRecipientModelNotFoundException;
8
use ByTIC\Notifier\Models\Events\EventTrait as Event;
9
use ByTIC\Notifier\Models\Messages\MessagesTrait;
10
use ByTIC\Notifier\Models\Recipients\Types\AbstractType;
11
use ByTIC\Notifier\Models\Topics\TopicTrait as Topic;
12
use ByTIC\Notifier\Models\Messages\MessageTrait as Message;
13
use ByTIC\Notifier\Models\Messages\MessagesTrait as Messages;
14
use Nip\Records\Locator\ModelLocator;
15
use Nip\Records\Record;
16
use Nip\Records\RecordManager as Records;
17
18
/**
19
 * Class RecipientTrait
20
 * @package ByTIC\Notifier\Models\Recipients
21
 *
22
 * @property int $id_topic
23
 * @property string $recipient
24
 * @property string $type
25
 * @property string $active
26
 *
27
 * @method AbstractType getType
28
 * @method Topic getTopic
29
 * @method RecipientsTrait getManager()
30
 */
31
trait RecipientTrait
32
{
33
    use RecordTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait ByTIC\Common\Records\Traits\HasTypes\RecordTrait has been deprecated: Use ByTIC\Models\SmartProperties\RecordsTraits\HasTypes\RecordTrait ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
    use /** @scrutinizer ignore-deprecated */ RecordTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
34
35
    protected $recipientManager = null;
36
37
    /**
38
     * @return string
39
     */
40 1
    public function getRecipient()
41
    {
42 1
        return $this->recipient;
43
    }
44
45
    /**
46
     * @return bool
47
     */
48 1
    public function isActive()
49
    {
50 1
        return $this->active == 'yes';
51
    }
52
53
    /**
54
     * @param $event
55
     * @return \ByTIC\Notifications\Notifiable[]
56
     * @throws NotificationRecipientModelNotFoundException
57
     * @throws NotificationModelNotFoundException
58
     */
59
    public function generateNotifiablesForEvent($event)
60
    {
61
        $notifiableModels = $this->getRecipientModelFromEvent($event);
62
        if ($notifiableModels) {
63
            /** @var IsRecipientTrait $notifiableModels */
64
            return $notifiableModels->generateNotifiables();
65
        }
66
67
        throw new NotificationRecipientModelNotFoundException(
68
            "No model found in recipient" . $this->getRecipient() . " from notification event [" . $event->id . "]"
69
        );
70
    }
71
72
    /**
73
     * @param Event $event
74
     * @return RecipientTrait
75
     * @throws NotificationModelNotFoundException
76
     */
77
    public function getRecipientModelFromEvent($event)
78
    {
79
        $method = $this->generateRecipientGetterMethod();
80
        $model = $event->getModel();
81
        if ($model instanceof Record) {
82
            return $model->$method();
83
        }
84
        return null;
85
    }
86
87
    /**
88
     * Return the Message from the database with the text to include
89
     * in the notification
90
     *
91
     * @param string $channel
92
     * @return Message
93
     */
94 1
    public function getNotificationMessage($channel = 'email')
95
    {
96
        /** @var MessagesTrait $messagesTable */
97 1
        $messagesTable = ModelLocator::get('Notifications\Messages');
98 1
        return $messagesTable::getGlobal(
99 1
            $this->id_topic,
100 1
            $this->getRecipient(),
101 1
            $channel
102
        );
103
    }
104
105
    /**
106
     * @return Records
107
     */
108
    public function getRecipientManager()
109
    {
110
        if ($this->recipientManager === null) {
111
            $this->recipientManager = $this->generateRecipientManager();
112
        }
113
        return $this->recipientManager;
114
    }
115
116
    /**
117
     * @return Records
118
     */
119
    public function generateRecipientManager()
120
    {
121
        return $this->getManager()::getRecipientManager($this->getRecipient());
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function generateRecipientGetterMethod()
128
    {
129
        return $this->getManager()::generateRecipientGetterMethod($this->getRecipient());
130
    }
131
}
132