Skip to content

Commit 79cfa1a

Browse files
Send 메소드 추가 및 모델 정리
1 parent 212b252 commit 79cfa1a

10 files changed

Lines changed: 211 additions & 110 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ out/
66
.idea/
77
config.php
88
*.real.php
9+
debug/
910

1011

1112
# Composer gitignore

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "solapi/solapi-php",
33
"description": "SOLAPI SDK for PHP",
4+
"version": "5.0.0",
45
"type": "library",
56
"license": "MIT",
67
"autoload": {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Nurigo\Solapi\Exceptions;
4+
5+
use Exception;
6+
7+
class SolapiCurlException extends Exception
8+
{
9+
10+
}

src/Libraries/Authenticator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ class Authenticator
66
{
77
public static function getAuthorizationHeaderInfo(string $apiKey, string $apiSecretKey): string
88
{
9-
date_default_timezone_set('Asia/Seoul');
10-
$date = date('Y-m-d\TH:i:s.Z\Z', time());
9+
// TODO: Timezone 문제가 생길 수 있어 추후 수정 필요
10+
date_default_timezone_set("Asia/Seoul");
11+
$date = date("Y-m-d\TH:i:s.Z\Z", time());
1112
$salt = uniqid();
12-
$signature = hash_hmac('sha256', $date . $salt, $apiSecretKey);
13+
$signature = hash_hmac("sha256", $date . $salt, $apiSecretKey);
1314
return "Authorization: HMAC-SHA256 apiKey={$apiKey}, date={$date}, salt={$salt}, signature={$signature}";
1415
}
1516
}

src/Libraries/Fetcher.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Nurigo\Solapi\Libraries;
44

55
use Exception;
6+
use Nurigo\Solapi\Exceptions\SolapiCurlException;
67

78
class Fetcher
89
{
@@ -11,7 +12,7 @@ class Fetcher
1112
protected $apiKey = '';
1213
protected $apiSecretKey = '';
1314

14-
const API_URL = 'https://api.solapi.com';
15+
const API_URL = "https://api.solapi.com";
1516

1617
public static function getInstance(string $apiKey, string $apiSecretKey)
1718
{
@@ -31,37 +32,42 @@ public function __destruct()
3132
$this->apiSecretKey = '';
3233
}
3334

34-
function request(string $method, string $uri, $data = false)
35+
/**
36+
* @param string $method
37+
* @param string $uri
38+
* @param mixed $data
39+
* @return mixed
40+
* @throws Exception|SolapiCurlException CURL 관련된 Exception
41+
*/
42+
public function request(string $method, string $uri, $data = false)
3543
{
3644
$authHeaderInfo = Authenticator::getAuthorizationHeaderInfo($this->apiKey, $this->apiSecretKey);
3745
$url = self::API_URL . $uri;
3846

39-
try {
40-
$curl = curl_init();
41-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
42-
switch ($method) {
43-
case "POST":
44-
case "PUT":
45-
case "DELETE":
46-
if ($data) curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
47-
break;
48-
case "GET":
49-
default:
50-
if ($data) $url = sprintf("%s?%s", $url, http_build_query($data));
51-
break;
52-
}
53-
$httpHeaders = array($authHeaderInfo, "Content-Type: application/json");
54-
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders);
55-
curl_setopt($curl, CURLOPT_URL, $url);
56-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
57-
if (curl_error($curl)) {
58-
print curl_error($curl);
59-
}
60-
$result = curl_exec($curl);
61-
curl_close($curl);
62-
return json_decode($result);
63-
} catch (Exception $err) {
64-
return $err;
47+
$curl = curl_init();
48+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
49+
switch ($method) {
50+
case "POST":
51+
case "PUT":
52+
case "DELETE":
53+
if ($data) curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
54+
break;
55+
case "GET":
56+
default:
57+
if ($data) $url = sprintf("%s?%s", $url, http_build_query($data));
58+
break;
6559
}
60+
$httpHeaders = array($authHeaderInfo, "Content-Type: application/json");
61+
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders);
62+
curl_setopt($curl, CURLOPT_URL, $url);
63+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
64+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
65+
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
66+
if (curl_error($curl)) {
67+
throw new SolapiCurlException(curl_error($curl));
68+
}
69+
$result = curl_exec($curl);
70+
curl_close($curl);
71+
return json_decode($result);
6672
}
6773
}
Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
<?php
22

3-
namespace Nurigo\Solapi\Models\kakao;
3+
namespace Nurigo\Solapi\Models\Kakao;
4+
5+
use stdClass;
46

57
class KakaoOption
68
{
9+
/**
10+
* @var string 카카오 채널 ID
11+
*/
712
public $pfId;
13+
14+
/**
15+
* @var string 카카오 알림톡 템플릿 ID
16+
*/
817
public $templateId;
9-
public $variables = null;
18+
19+
/**
20+
* @var stdClass|null 치환문구 처리변수
21+
*/
22+
public $variables;
23+
24+
/**
25+
* @var bool 대체 발송 비활성화 옵션
26+
* true일 경우에만 대체 발송이 비활성화 됩니다.
27+
*/
1028
public $disableSms = false;
29+
30+
/**
31+
* @var bool
32+
*/
1133
public $adFlag = false;
34+
35+
/**
36+
* @var array 메시지 버튼 목록
37+
*/
1238
public $buttons = array();
13-
public $imageId;
1439

1540
/**
1641
* @return string
@@ -120,21 +145,9 @@ public function setButtons(array $buttons): KakaoOption
120145
return $this;
121146
}
122147

123-
/**
124-
* @return string
125-
*/
126-
public function getImageId(): string
127-
{
128-
return $this->imageId;
129-
}
130-
131-
/**
132-
* @param string $imageId
133-
* @return KakaoOption
134-
*/
135-
public function setImageId(string $imageId): KakaoOption
148+
public function __construct()
136149
{
137-
$this->imageId = $imageId;
138-
return $this;
150+
$this->variables = new stdClass();
151+
unset($this->buttons);
139152
}
140153
}

src/Models/Message.php

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@
22

33
namespace Nurigo\Solapi\Models;
44

5-
use Nurigo\Solapi\Models\kakao\KakaoOption;
5+
use Nurigo\Solapi\Models\Kakao\KakaoOption;
66

7+
// TODO: Property 설명 정의해야 함
78
class Message
89
{
10+
/**
11+
* @var string 수신번호
12+
*/
913
public $to;
14+
15+
/**
16+
* @var string 발신번호
17+
*/
1018
public $from;
19+
20+
/**
21+
* @var string 메시지 내용(SMS, LMS, MMS, CTA, CTI 전용)
22+
*/
1123
public $text;
12-
public $groupId;
13-
public $messageId;
24+
25+
/**
26+
* @var string 스토리지 내 이미지 ID
27+
*/
1428
public $imageId;
15-
public $type;
1629
public $subject;
17-
public $autoTypeDetect = true;
1830
public $country = "82";
1931

2032
public $kakaoOptions;
2133

22-
public $logs = array();
23-
24-
public $dateCreated;
25-
public $dateUpdated;
26-
2734
/**
2835
* @return string
2936
*/
@@ -222,57 +229,11 @@ public function setKakaoOptions(KakaoOption $kakaoOptions): Message
222229
return $this;
223230
}
224231

225-
/**
226-
* @return array
227-
*/
228-
public function getLogs(): array
229-
{
230-
return $this->logs;
231-
}
232-
233-
/**
234-
* @param array $logs
235-
* @return Message
236-
*/
237-
public function setLogs(array $logs): Message
238-
{
239-
$this->logs = $logs;
240-
return $this;
241-
}
242-
243-
/**
244-
* @return string
245-
*/
246-
public function getDateCreated(): string
247-
{
248-
return $this->dateCreated;
249-
}
250-
251-
/**
252-
* @param string $dateCreated
253-
* @return Message
254-
*/
255-
public function setDateCreated(string $dateCreated): Message
256-
{
257-
$this->dateCreated = $dateCreated;
258-
return $this;
259-
}
260-
261-
/**
262-
* @return string
263-
*/
264-
public function getDateUpdated(): string
265-
{
266-
return $this->dateUpdated;
267-
}
268-
269-
/**
270-
* @param string $dateUpdated
271-
* @return Message
272-
*/
273-
public function setDateUpdated(string $dateUpdated): Message
232+
public function __construct()
274233
{
275-
$this->dateUpdated = $dateUpdated;
276-
return $this;
234+
unset($this->text);
235+
unset($this->kakaoOptions);
236+
unset($this->subject);
237+
unset($this->imageId);
277238
}
278239
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Nurigo\Solapi\Models\Request;
4+
5+
class DefaultAgent
6+
{
7+
/**
8+
* @var string PHP SDK 버전
9+
*/
10+
public $sdkVersion;
11+
12+
/**
13+
* @var string SDK 사용자 OS 버전
14+
*/
15+
public $osPlatform;
16+
17+
public function __construct()
18+
{
19+
$this->sdkVersion = 'php/5.0.0';
20+
$this->osPlatform = PHP_OS . " | " . phpversion();
21+
}
22+
}

src/Models/Request/SendRequest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Nurigo\Solapi\Models\Request;
4+
5+
use DateTime;
6+
use Nurigo\Solapi\Libraries\SolapiSerializer;
7+
use Nurigo\Solapi\Models\Message;
8+
9+
class SendRequest
10+
{
11+
/**
12+
* @var Message[] 메시지를 발송할 객체
13+
*/
14+
public $messages;
15+
16+
/**
17+
* @var string 예약일시
18+
*/
19+
public $scheduledDate;
20+
21+
/**
22+
* @var DefaultAgent SDK 정보를 담은 객체
23+
*/
24+
public $agent;
25+
26+
/**
27+
* @var bool 중복 수신번호 허용여부, 기본 값은 비허용
28+
*/
29+
public $allowDuplicates = false;
30+
31+
/**
32+
* @param Message[] $messages
33+
* @param DateTime|null $scheduledDate
34+
* @param bool $allowDuplicates
35+
*/
36+
public function __construct(array $messages, DateTime $scheduledDate = null, bool $allowDuplicates = false)
37+
{
38+
$this->messages = $messages;
39+
if ($scheduledDate != null) {
40+
$this->scheduledDate = $scheduledDate->format('c');
41+
} else {
42+
unset($this->scheduledDate);
43+
}
44+
$this->agent = new DefaultAgent();
45+
$this->allowDuplicates = $allowDuplicates;
46+
}
47+
}

0 commit comments

Comments
 (0)