Completed
Push — master ( 5e25c8...dae486 )
by Abdelrahman
02:22
created

src/AuthyChannel.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NotificationChannels\Authy;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Illuminate\Notifications\Notification;
7
use NotificationChannels\Authy\Exceptions\InvalidConfiguration;
8
9
class AuthyChannel
10
{
11
    /**
12
     * The Authy production API endpoint.
13
     *
14
     * @var string
15
     */
16
    const API_ENDPOINT_PRODUCTION = 'https://api.authy.com';
17
18
    /**
19
     * The Authy sandbox API endpoint.
20
     *
21
     * @var string
22
     */
23
    const API_ENDPOINT_SANDBOX = 'http://sandbox-api.authy.com';
24
25
    /**
26
     * The HTTP client instance.
27
     *
28
     * @var \GuzzleHttp\Client
29
     */
30
    protected $http;
31
32
    /**
33
     * The Authy service key.
34
     *
35
     * @var string
36
     */
37
    protected $key;
38
39
    /**
40
     * The Authy service API endpoint.
41
     *
42
     * @var string
43
     */
44
    protected $api;
45
46
    /**
47
     * Create a new Slack channel instance.
48
     *
49
     * @param \GuzzleHttp\Client $http
50
     *
51
     * @throws \NotificationChannels\Authy\Exceptions\InvalidConfiguration
52
     */
53
    public function __construct(HttpClient $http)
54
    {
55
        $this->http = $http;
56
57
        // Prepare required data
58
        $mode = config('services.authy.mode');
59
        $this->key = config('services.authy.keys.'.$mode);
60
        $this->api = $mode === 'sandbox' ? static::API_ENDPOINT_SANDBOX : static::API_ENDPOINT_PRODUCTION;
61
62
        // Check configuration
63
        if (! $mode || ! $this->key) {
64
            throw InvalidConfiguration::missingCredentials();
65
        }
66
    }
67
68
    /**
69
     * Send the given notification.
70
     *
71
     * @param mixed                                  $notifiable
72
     * @param \Illuminate\Notifications\Notification $notification
73
     *
74
     * @return bool
75
     */
76
    public function send($notifiable, Notification $notification)
77
    {
78
        if (! $authyId = $notifiable->routeNotificationFor('authy')) {
79
            return false;
80
        }
81
82
        $message = $notification->toAuthy($notifiable);
1 ignored issue
show
The method toAuthy() 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...
83
84
        // Prepare required data
85
        $force = $message->force ? '&force=true' : '';
86
        $url = $this->api.'/protected/json/'.$message->method.'/'.$authyId.'?api_key='.$this->key.$force;
87
88
        // Send Authy notification
89
        $response = json_decode($this->http->get($url)->getBody(), true);
90
91
        return isset($response['success']) && $response['success'];
92
    }
93
}
94