Completed
Push — master ( 7bba81...ffc185 )
by Kittinan
03:58
created

LineNotify::getToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace KS\Line;
4
5
use GuzzleHttp\Client;
6
7
/**
8
 * A simple class to send text message, image and Line sticker on Line notify
9
 * 
10
 * https://notify-bot.line.me/doc/en/
11
 * 
12
 * @property string $token Line notify token
13
 * @property \GuzzleHttp\Client $http Guzzle Http Client instance for send Http request
14
 */
15
class LineNotify {
16
17
  const API_URL = 'https://notify-api.line.me/api/notify';
18
19
  private $token = null;
20
  private $http = null;
21
  
22
  /**
23
   * Initialize class with Line notify token string
24
   * 
25
   * @param string $token the token of Line notify
26
   */
27
  public function __construct($token) {
28
29
    $this->token = $token;
30
    $this->http = new Client();
31
  }
32
  
33
  /**
34
   * Set token Line notify that want to send message
35
   * 
36
   * @param string $token the token of Line notify
37
   */
38 1
  public function setToken($token) {
39 1
    $this->token = $token;
40 1
  }
41
  
42
  /**
43
   * Get current token Line Notify
44
   * 
45
   * @return string the token of Line notify
46
   */
47 2
  public function getToken() {
48 2
    return $this->token;
49
  }
50
  
51
  /**
52
   * Send text message, image or sticker on Line notify
53
   * 
54
   * @param string $text text message on Line notify can not be empty
55
   * @param string $imagePath image path you want to send on the local machine
56
   * @param array() $sticker array of line sticker ['stickerPackageId' => PACKAGE_ID, 'stickerId' => STICKER_ID ] 
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
57
   *                         more info https://devdocs.line.me/files/sticker_list.pdf
58
   * @return boolean success or fail on send Line notify message
59
   */
60
  public function send($text, $imagePath = null, $sticker = null) {
61
62
    if (empty($text)) {
63
      return false;
64
    }
65
66
    $request_params = [
67
      'headers' => [
68
        'Authorization' => 'Bearer ' . $this->token,
69
      ],
70
    ];
71
    
72
    //Message always required
73
    $request_params['multipart'] = [
74
      [
75
        'name' => 'message',
76
        'contents' => $text
77
      ]
78
    ];
79
80
    if (!empty($imagePath)) {
81
      $request_params['multipart'][] = [
82
        'name' => 'imageFile',
83
        'contents' => fopen($imagePath, 'r')
84
      ];
85
    }
86
87
    //https://devdocs.line.me/files/sticker_list.pdf
88
    if (!empty($sticker) 
89
      && !empty($sticker['stickerPackageId']) 
90
      && !empty($sticker['stickerId'])) {
91
      
92
      $request_params['multipart'][] = [
93
        'name' => 'stickerPackageId',
94
        'contents' => $sticker['stickerPackageId']
95
      ];
96
      
97
      $request_params['multipart'][] = [
98
        'name' => 'stickerId',
99
        'contents' => $sticker['stickerId']
100
      ];
101
      
102
    }
103
104
    $response = $this->http->request('POST', LineNotify::API_URL, $request_params);
105
106
    if ($response->getStatusCode() != 200) {
107
      return false;
108
    }
109
110
    $body = (string) $response->getBody();
111
    $json = json_decode($body, true);
112
    if (empty($json['status']) || empty($json['message'])) {
113
      return false;
114
    }
115
116
    return true;
117
  }
118
119
}
120