Skip to content

Commit 219779e

Browse files
[HSDK-1] PSR-2
1 parent 8e4df0d commit 219779e

File tree

3 files changed

+173
-149
lines changed

3 files changed

+173
-149
lines changed

src/Ecommerce.php

Lines changed: 166 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Sipay;
44

5-
class Ecommerce{
5+
class Ecommerce
6+
{
67
protected $logger;
78
protected $key;
89
protected $secret;
@@ -12,8 +13,9 @@ class Ecommerce{
1213
protected $mode;
1314
protected $timeout;
1415

15-
public function __construct($config_path){
16-
if (gettype($config_path) != "string"){
16+
public function __construct($config_path)
17+
{
18+
if (gettype($config_path) != "string") {
1719
throw new \Exception('$config_path must be a string.');
1820
}
1921

@@ -35,170 +37,187 @@ public function __construct($config_path){
3537
$this->setTimeout(isset($connection['timeout']) ? $connection['timeout'] : '30');
3638
}
3739

38-
public function getKey(){
39-
return $this->key;
40-
}
41-
42-
public function setKey($key){
43-
if (gettype($key) != "string"){
44-
throw new \Exception('$key must be a string.');
45-
}
46-
if(!preg_match("/^[\w-]{6,32}$/", $key)){
47-
throw new \Exception('$key don\'t match with pattern.');
48-
}
49-
$this->key = $key;
50-
}
51-
52-
public function getSecret(){
53-
return $this->secret;
54-
}
55-
56-
public function setSecret($secret){
57-
if (gettype($secret) != "string"){
58-
throw new \Exception('$secret must be a string.');
59-
}
60-
if(!preg_match("/^[\w-]{6,32}$/", $secret)){
61-
throw new \Exception('$secret don\'t match with pattern.');
62-
}
63-
$this->secret = $secret;
64-
}
65-
66-
public function getResource(){
67-
return $this->resource;
68-
}
69-
70-
public function setResource($resource){
71-
if (gettype($resource) != "string"){
72-
throw new \Exception('$resource must be a string.');
73-
}
74-
if(!preg_match("/^[\w-]{6,32}$/", $resource)){
75-
throw new \Exception('$resource don\'t match with pattern.');
76-
}
77-
$this->resource = $resource;
78-
}
79-
80-
public function getEnvironment(){
81-
return $this->environment;
82-
}
83-
84-
public function setEnvironment($environment){
85-
if (gettype($environment) != "string"){
86-
throw new \Exception('$environment must be a string.');
87-
}
88-
if(!in_array($environment, array('develop','sandbox', 'staging', 'live'))){
89-
throw new \Exception('$environment must be sandbox, staging or live.');
90-
}
91-
$this->environment = $environment;
92-
}
93-
94-
public function getVersion(){
95-
return $this->version;
96-
}
97-
98-
public function setVersion($version){
99-
if (gettype($version) != "string"){
100-
throw new \Exception('$version must be a string.');
101-
}
102-
if($version != "v1"){
103-
throw new \Exception('$version must be v1.');
104-
}
105-
$this->version = $version;
106-
}
107-
108-
public function getMode(){
109-
return $this->mode;
110-
}
111-
112-
public function setMode($mode){
113-
if (gettype($mode) != "string"){
114-
throw new \Exception('$mode must be a string.');
115-
}
116-
if(!in_array($mode, array('sha256','sha512'))){
117-
throw new \Exception('$mode must be sha256 or sha512.');
118-
}
119-
$this->mode = $mode;
120-
}
121-
122-
public function getTimeout(){
123-
return $this->timeout;
124-
}
125-
126-
public function setTimeout($timeout){
127-
if (!is_numeric($timeout)){
128-
throw new \Exception('$timeout must be a integer.');
129-
}
130-
$timeout = intval($timeout);
131-
if($timeout <= 0){
132-
throw new \Exception('$timeout must be geater than 0.');
133-
}
134-
$this->timeout = $timeout;
135-
}
136-
137-
private function send($payload, $endpoint){
138-
$url = 'https://'.$this->environment.'.sipay.es/mdwr/'.$this->version.'/'.$endpoint;
139-
$data = array(
40+
public function getKey()
41+
{
42+
return $this->key;
43+
}
44+
45+
public function setKey($key)
46+
{
47+
if (gettype($key) != "string") {
48+
throw new \Exception('$key must be a string.');
49+
}
50+
if(!preg_match("/^[\w-]{6,32}$/", $key)) {
51+
throw new \Exception('$key don\'t match with pattern.');
52+
}
53+
$this->key = $key;
54+
}
55+
56+
public function getSecret()
57+
{
58+
return $this->secret;
59+
}
60+
61+
public function setSecret($secret)
62+
{
63+
if (gettype($secret) != "string") {
64+
throw new \Exception('$secret must be a string.');
65+
}
66+
if(!preg_match("/^[\w-]{6,32}$/", $secret)) {
67+
throw new \Exception('$secret don\'t match with pattern.');
68+
}
69+
$this->secret = $secret;
70+
}
71+
72+
public function getResource()
73+
{
74+
return $this->resource;
75+
}
76+
77+
public function setResource($resource)
78+
{
79+
if (gettype($resource) != "string") {
80+
throw new \Exception('$resource must be a string.');
81+
}
82+
if(!preg_match("/^[\w-]{6,32}$/", $resource)) {
83+
throw new \Exception('$resource don\'t match with pattern.');
84+
}
85+
$this->resource = $resource;
86+
}
87+
88+
public function getEnvironment()
89+
{
90+
return $this->environment;
91+
}
92+
93+
public function setEnvironment($environment)
94+
{
95+
if (gettype($environment) != "string") {
96+
throw new \Exception('$environment must be a string.');
97+
}
98+
if(!in_array($environment, array('develop','sandbox', 'staging', 'live'))) {
99+
throw new \Exception('$environment must be sandbox, staging or live.');
100+
}
101+
$this->environment = $environment;
102+
}
103+
104+
public function getVersion()
105+
{
106+
return $this->version;
107+
}
108+
109+
public function setVersion($version)
110+
{
111+
if (gettype($version) != "string") {
112+
throw new \Exception('$version must be a string.');
113+
}
114+
if($version != "v1") {
115+
throw new \Exception('$version must be v1.');
116+
}
117+
$this->version = $version;
118+
}
119+
120+
public function getMode()
121+
{
122+
return $this->mode;
123+
}
124+
125+
public function setMode($mode)
126+
{
127+
if (gettype($mode) != "string") {
128+
throw new \Exception('$mode must be a string.');
129+
}
130+
if(!in_array($mode, array('sha256','sha512'))) {
131+
throw new \Exception('$mode must be sha256 or sha512.');
132+
}
133+
$this->mode = $mode;
134+
}
135+
136+
public function getTimeout()
137+
{
138+
return $this->timeout;
139+
}
140+
141+
public function setTimeout($timeout)
142+
{
143+
if (!is_numeric($timeout)) {
144+
throw new \Exception('$timeout must be a integer.');
145+
}
146+
$timeout = intval($timeout);
147+
if($timeout <= 0) {
148+
throw new \Exception('$timeout must be geater than 0.');
149+
}
150+
$this->timeout = $timeout;
151+
}
152+
153+
private function send($payload, $endpoint)
154+
{
155+
$url = 'https://'.$this->environment.'.sipay.es/mdwr/'.$this->version.'/'.$endpoint;
156+
$data = array(
140157
'key' => $this->key,
141158
'nonce' => "".time(),
142159
'mode' => $this->mode,
143160
'resource' => $this->resource,
144161
'payload' => $payload
145-
);
162+
);
146163

147-
$body = json_encode($data);
148-
$signature = hash_hmac($this->mode, $body, $this->secret);
164+
$body = json_encode($data);
165+
$signature = hash_hmac($this->mode, $body, $this->secret);
149166

150-
$options = array(
151-
'http' => array(
167+
$options = array(
168+
'http' => array(
152169
'header' => "Content-type: application/json\r\nContent-Signature: ".$signature."\r\n",
153170
'method' => 'POST',
154171
'content' => $body,
155172
'timeout' => $this->timeout,
156173
'ignore_errors' => true
157-
),
158-
);
159-
$context = stream_context_create($options);
160-
try {
161-
$response_body = file_get_contents($url, false, $context);
162-
} catch (Exception $e) {
163-
$this->logger->error('sipay.request', 'request.response', 'E-0004', 'Request Error',
164-
array('error_msg' => $e->getMessage()));
165-
$response = null;
166-
$response_body = null;
167-
}
168-
169-
if(!is_null($response_body)){
170-
if(isset($http_response_header)){
171-
if($response_body == ""){
172-
$this->logger->error('sipay.request', 'request.response', 'E-0001', 'Request Error', array());
174+
),
175+
);
176+
$context = stream_context_create($options);
177+
try {
178+
$response_body = file_get_contents($url, false, $context);
179+
} catch (Exception $e) {
180+
$this->logger->error(
181+
'sipay.request', 'request.response', 'E-0004', 'Request Error',
182+
array('error_msg' => $e->getMessage())
183+
);
173184
$response = null;
185+
$response_body = null;
186+
}
174187

175-
}else{
176-
$is_json = false;
177-
foreach($http_response_header as $header){
178-
if(substr($header, 0, 30) === "Content-Type: application/json"){
179-
$is_json = true;
180-
break;
181-
}
182-
}
183-
184-
if($is_json){
185-
$response = json_decode($response_body, True);
188+
if(!is_null($response_body)) {
189+
if(isset($http_response_header)) {
190+
if($response_body == "") {
191+
$this->logger->error('sipay.request', 'request.response', 'E-0001', 'Request Error', array());
192+
$response = null;
193+
194+
}else{
195+
$is_json = false;
196+
foreach($http_response_header as $header){
197+
if(substr($header, 0, 30) === "Content-Type: application/json") {
198+
$is_json = true;
199+
break;
200+
}
201+
}
202+
203+
if($is_json) {
204+
$response = json_decode($response_body, true);
205+
206+
}else{
207+
$this->logger->error('sipay.request', 'request.response', 'E-0003', 'Response no json', array());
208+
$response = null;
209+
}
210+
}
186211

187212
}else{
188-
$this->logger->error('sipay.request', 'request.response', 'E-0003', 'Response no json', array());
189-
$response = null;
213+
$this->logger->error('sipay.request', 'request.response', 'E-0002', 'Request Error', array());
214+
$response = null;
190215
}
191-
}
192-
193-
}else{
194-
$this->logger->error('sipay.request', 'request.response', 'E-0002', 'Request Error', array());
195-
$response = null;
196216
}
197-
}
198217

199-
return array($body, $response);
200-
}
218+
return array($body, $response);
201219
}
220+
}
202221

203222
return $response;
204223
}

src/Logger.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Logger
1818

1919
/**
2020
* Log Levels
21+
*
2122
* @var array
2223
*/
2324
protected static $levels = array(
@@ -93,7 +94,11 @@ protected function flush()
9394
}
9495
}
9596

96-
usort($logs, function($a, $b) {return ($a['date'] < $b['date']) ? -1 : 1;});
97+
usort(
98+
$logs, function ($a, $b) {
99+
return ($a['date'] < $b['date']) ? -1 : 1;
100+
}
101+
);
97102

98103
$delete = (count($logs) > $this->options['backup_file_rotation']) ? count($logs) - $this->options['backup_file_rotation'] : 0;
99104

src/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function autoload($class)
2727
$path .= $filename;
2828

2929
if (file_exists($path)) {
30-
require $path;
30+
include $path;
3131
}
3232
}
3333

0 commit comments

Comments
 (0)