Passed
Pull Request — master (#75)
by Nathan
05:29
created

EntitySlackBotMapper::getBot()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Service;
18
19
use CuyZ\Notiz\Core\Channel\Payload;
20
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
21
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Data\SlackBot;
22
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\EntitySlackNotification;
23
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Settings\EntitySlackSettings;
24
25
class EntitySlackBotMapper
26
{
27
    /**
28
     * @var EntitySlackNotification
29
     */
30
    private $notification;
31
32
    /**
33
     * @var EntitySlackSettings
34
     */
35
    private $notificationSettings;
36
37
    /**
38
     * @param Payload $payload
39
     */
40
    public function __construct(Payload $payload)
41
    {
42
        $this->notification = $payload->getNotification();
43
        $this->notificationSettings = $payload->getNotificationDefinition()->getSettings();
44
    }
45
46
    /**
47
     * Returns either a custom bot or a defined one.
48
     *
49
     * @return SlackBot
50
     * @throws EntryNotFoundException
51
     */
52
    public function getBot()
53
    {
54
        if ($this->notification->hasCustomBot()) {
55
            return SlackBot::fromNotification($this->notification);
56
        }
57
58
        $botIdentifier = $this->notification->getBot();
59
60
        foreach ($this->notificationSettings->getBots() as $bot) {
61
            if ($bot->getIdentifier() === $botIdentifier) {
62
                return SlackBot::fromBotDefinition($bot);
63
            }
64
        }
65
66
        throw EntryNotFoundException::entitySlackBotNotFound($botIdentifier);
67
    }
68
}
69