LineNotify::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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) && preg_match("#^https?://#", $imagePath)) {
81
      // Remote HTTP / HTTPS image
82
      $request_params['multipart'][] = [
83
        'name' => 'imageThumbnail',
84
        'contents' => $imagePath
85
      ];
86
      
87
      $request_params['multipart'][] = [
88
        'name' => 'imageFullsize',
89
        'contents' => $imagePath
90
      ];
91
      
92
    } elseif (!empty($imagePath) && file_exists($imagePath)) {
93
      // Local image
94
      $request_params['multipart'][] = [
95
        'name' => 'imageFile',
96
        'contents' => fopen($imagePath, 'r')
97
      ];
98
    }
99
100
    //https://devdocs.line.me/files/sticker_list.pdf
101
    if (!empty($sticker) 
102
      && !empty($sticker['stickerPackageId']) 
103
      && !empty($sticker['stickerId'])) {
104
      
105
      $request_params['multipart'][] = [
106
        'name' => 'stickerPackageId',
107
        'contents' => $sticker['stickerPackageId']
108
      ];
109
      
110
      $request_params['multipart'][] = [
111
        'name' => 'stickerId',
112
        'contents' => $sticker['stickerId']
113
      ];
114
      
115
    }
116
117
    $response = $this->http->request('POST', LineNotify::API_URL, $request_params);
118
119
    if ($response->getStatusCode() != 200) {
120
      return false;
121
    }
122
123
    $body = (string) $response->getBody();
124
    $json = json_decode($body, true);
125
    if (empty($json['status']) || empty($json['message'])) {
126
      return false;
127
    }
128
129
    return true;
130
  }
131
132
}
133