Completed
Push — master ( 03ffd9...438aee )
by Freek
12:35
created

EvernoteChannel::getContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace NotificationChannels\Evernote;
4
5
use Evernote\Client;
6
use Evernote\Model\HtmlNoteContent;
7
use Evernote\Model\Note;
8
use Evernote\Model\PlainTextNoteContent;
9
use Exception;
10
use Illuminate\Support\Arr;
11
use NotificationChannels\Evernote\Exceptions\CouldNotSendNotification;
12
use Illuminate\Notifications\Notification;
13
14
class EvernoteChannel
15
{
16
    /** @var Client */
17
    protected $client;
18
19
    /**
20
     * @param Client $client
21
     */
22 2
    public function __construct(Client $client)
23
    {
24 2
        $this->client = $client;
25 2
    }
26
27
    /**
28
     * Send the given notification.
29
     *
30
     * @param mixed $notifiable
31
     * @param \Illuminate\Notifications\Notification $notification
32
     *
33
     * @throws \NotificationChannels\Evernote\Exceptions\CouldNotSendNotification
34
     */
35 2
    public function send($notifiable, Notification $notification)
36
    {
37 2
        if (!$token = $notifiable->routeNotificationFor('Evernote')) {
38
            return;
39
        }
40
41 2
        $this->client->setToken($token);
42
43 2
        $message = $notification->toEvernote($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toEvernote() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
45 2
        $this->client->setSandbox($message['sandbox'] ?: false);
46
47 2
        $note = $this->convertMessageToNote($message);
48 2
49
        try {
50 2
            $this->client->uploadNote($note);
51 2
        } catch (Exception $exception) {
52
            throw CouldNotSendNotification::serviceRespondedWithAnError($exception->getMessage());
53
        }
54 2
    }
55
56 2
57 2
    /**
58 2
     * @param array $message
59
     *
60 2
     * @return \Evernote\Model\Note
61 2
     */
62 2
    protected function convertMessageToNote($message)
63
    {
64 2
        $note = new Note();
65 2
66 2
        $note->setTitle($message['title']);
67
68
        if ($content = $this->getContent($message)) {
69 2
            $note->setContent($content);
70 2
        }
71 1
72
        $note->setTagNames($message['tags']);
73 1
74
        if ($message['done'] === true) {
75
            $note->setAsDone();
76
        }
77
78
        $note->setReminder($message['reminder'] ?: false);
79
80
        return $note;
81
    }
82
83
84
    /**
85
     * @param $message
86
     * @return \Evernote\Model\HtmlNoteContent|\Evernote\Model\PlainTextNoteContent|null
87
     */
88
    protected function getContent($message)
89
    {
90
        if (!is_null($message['content'])) {
91
            return null;
92
        }
93
94
        if (Arr::get($message, 'content.type') === EvernoteContent::TYPE_HTML) {
95
            $content = new HtmlNoteContent(Arr::get($message, 'content.content'));
96
            return $content;
97
        }
98
99
        $content = new PlainTextNoteContent(Arr::get($message, 'content.content'));
100
101
        return $content;
102
103
    }
104
}
105