EvernoteChannel::getContent()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.1971

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
ccs 3
cts 8
cp 0.375
crap 5.1971
rs 9.7333
c 0
b 0
f 0
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
49
        try {
50 2
            $this->client->uploadNote($note);
51 2
        } catch (Exception $exception) {
52 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($exception->getMessage());
53
        }
54 1
    }
55
56
    /**
57
     * @param array $message
58
     *
59
     * @return \Evernote\Model\Note
60
     */
61 2
    protected function convertMessageToNote($message)
62
    {
63 2
        $note = new Note();
64
65 2
        $note->setTitle($message['title']);
66
67 2
        if ($content = $this->getContent($message)) {
68
            $note->setContent($content);
69
        }
70
71 2
        $note->setTagNames($message['tags']);
72
73 2
        if ($message['done'] === true) {
74 2
            $note->setAsDone();
75 2
        }
76
77 2
        $note->setReminder($message['reminder'] ?: false);
78
79 2
        return $note;
80
    }
81
82
    /**
83
     * @param $message
84
     *
85
     * @return \Evernote\Model\HtmlNoteContent|\Evernote\Model\PlainTextNoteContent|null
86
     */
87 2
    protected function getContent($message)
88
    {
89 2
        if (! is_null($message['content'])) {
90 2
            return;
91
        }
92
93
        if (Arr::get($message, 'content.type') === EvernoteContent::TYPE_HTML) {
94
            $content = new HtmlNoteContent(Arr::get($message, 'content.content'));
95
96
            return $content;
97
        }
98
99
        $content = new PlainTextNoteContent(Arr::get($message, 'content.content'));
100
101
        return $content;
102
    }
103
}
104