Passed
Push — master ( 66351d...335aef )
by Hirofumi
20:01
created

SlackGateway::sendsToDestination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Infrastructure\Domain\Model;
5
6
use Maknz\Slack\Client;
7
use Maknz\Slack\Message;
8
use Shippinno\Notification\Domain\Model\Destination;
9
use Shippinno\Notification\Domain\Model\Gateway;
10
use Shippinno\Notification\Domain\Model\Notification;
11
use Shippinno\Notification\Domain\Model\NotificationNotSentException;
12
use Shippinno\Notification\Domain\Model\SlackChannelDestination;
13
use Throwable;
14
15
class SlackGateway extends Gateway
16
{
17
    /**
18
     * @var Client
19
     */
20
    private $client;
21
22
    /**
23
     * @param Client $client
24
     */
25 1
    public function __construct(Client $client)
26
    {
27 1
        $this->client = $client;
28 1
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    protected function doSend(Notification $notification): void
34
    {
35
        /** @var SlackChannelDestination $destination */
36 1
        $destination = $notification->destination();
37 1
        $text = $notification->subject()->subject()."\n\n";
38 1
        $text .= $notification->body()->body();
39 1
        $message = new Message($this->client);
40 1
        $message->setAllowMarkdown(true);
41 1
        $message->setText($text);
42 1
        if ($destination instanceof SlackChannelDestination) {
43 1
            $message->setChannel($destination->channel());
44
        }
45
        try {
46 1
            $message->send();
47
        } catch (Throwable $e) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
48
            throw new NotificationNotSentException($notification, $e);
49
        }
50 1
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function sendsToDestination(Destination $destination): bool
56
    {
57 1
        return $destination instanceof SlackChannelDestination;
58
    }
59
}
60