Completed
Pull Request — master (#104)
by Tyler
02:49
created

Pushover::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Backup\Notifications\Senders;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Spatie\Backup\Notifications\BaseSender;
7
8
class Pushover extends BaseSender
9
{
10
    /** @var array */
11
    protected $config;
12
13
    /**
14
     * Sounds the api supports by default
15
     * @see https://pushover.net/api#sounds
16
     * @var array
17
     */
18
    private $sounds = array(
19
        'pushover', 'bike', 'bugle', 'cashregister', 'classical', 'cosmic', 'falling', 'gamelan', 'incoming',
20
        'intermission', 'magic', 'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat', 'alien', 'climb',
21
        'persistent', 'echo', 'updown', 'none',
22
    );
23
24
    /**
25
     * @param Repository $config
26
     */
27
    public function __construct(Repository $config)
28
    {
29
        $this->config = $config->get('laravel-backup.notifications.pushover');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('laravel-ba...otifications.pushover') of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
    }
31
32
    /**
33
     * Sends the message to the Pushover API
34
     * @return void
35
     */
36
    public function send()
37
    {
38
        curl_setopt_array($ch = curl_init(), [
39
            CURLOPT_URL => 'https://api.pushover.net/1/messages.json',
40
            CURLOPT_POSTFIELDS => [
41
                'token' => $this->config['token'],
42
                'user' => $this->config['user'],
43
                'title' => $this->subject,
44
                'message' => $this->message,
45
                'sound' => $this->getSound(),
46
            ],
47
            CURLOPT_SAFE_UPLOAD => true,
48
        ]);
49
        curl_exec($ch);
50
        curl_close($ch);
51
    }
52
53
    /**
54
     * Returns the proper sound for the notification type according to the config file
55
     * @return string The sound string to use
56
     */
57
    private function getSound()
58
    {
59
        $sounds = isset($this->config['sounds']) ? $this->config['sounds'] : ['success' => 'pushover', 'error' => 'siren'];
60
61
        $sound = $this->type === static::TYPE_SUCCESS ? $sounds['success'] : $sounds['error'];
62
63
        if(!$this->isSupportedSound($sound)){
64
            $sound = 'pushover';
65
        }
66
67
        return $sound;
68
    }
69
70
    /**
71
     * Checks if the sound is supported by Pushover
72
     * @param  string  $sound The sound string to check
73
     * @return boolean        [description]
74
     */
75
    private function isSupportedSound($sound){
76
        return in_array($sound, $this->sounds);
77
    }
78
}
79