Passed
Pull Request — master (#75)
by Nathan
03:50
created

EntitySlackChannelMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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\Domain\Notification\Slack\Application\EntitySlack\Data\SlackChannel;
21
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\EntitySlackNotification;
22
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Settings\EntitySlackSettings;
23
24
class EntitySlackChannelMapper
25
{
26
    /**
27
     * @var EntitySlackNotification
28
     */
29
    private $notification;
30
31
    /**
32
     * @var EntitySlackSettings
33
     */
34
    private $notificationSettings;
35
36
    /**
37
     * @param Payload $payload
38
     */
39
    public function __construct(Payload $payload)
40
    {
41
        $this->notification = $payload->getNotification();
42
        $this->notificationSettings = $payload->getNotificationDefinition()->getSettings();
43
    }
44
45
    /**
46
     * Returns an array of channels from a custom one and/or from one or
47
     * more defined ones.
48
     *
49
     * @return SlackChannel[]
50
     */
51
    public function getChannels()
52
    {
53
        $channels = [];
54
55
        if ($this->notification->hasCustomSlackChannel()) {
56
            $channels[] = SlackChannel::fromNotification($this->notification);
57
        }
58
59
        $identifiers = explode(',', $this->notification->getSlackChannel());
60
61
        foreach ($identifiers as $identifier) {
62
            if (!$this->notificationSettings->hasChannel($identifier)) {
63
                continue;
64
            }
65
66
            $channels[] = SlackChannel::fromDefinition(
67
                $this->notificationSettings->getChannel($identifier)
68
            );
69
        }
70
71
        return $channels;
72
    }
73
}
74