Skip to content

Commit bf488e1

Browse files
committed
Fix style
1 parent 9479040 commit bf488e1

15 files changed

+330
-328
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[*]
2+
end_of_line = lf
3+
insert_final_newline = true
4+
indent_size = 4
5+
6+
[*.json]
7+
indent_style = space
8+
9+
[*.php]
10+
indent_style = tab
11+

src/WorkerJS/PHPClient/Client.php

Lines changed: 94 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,96 +13,101 @@
1313
use WorkerJS\PHPClient\exceptions\InvalidApiException;
1414

1515
class Client{
16-
private $options;
17-
18-
private $taskMessageRouter;
19-
private $taskStore;
20-
21-
private $defaultOptions = [
22-
"api" => "httpclient",
23-
"store" => [
24-
"type" => "mysql",
25-
"uri" => "mysql://root:toor@localhost/dbname"
26-
]
27-
];
28-
29-
/**
30-
* Client constructor.
31-
* @param $options
32-
* @throws StoreNotImplementedException
33-
*/
34-
public function __construct($options){
35-
$this->options = $options;
36-
37-
$this->taskMessageRouter = TaskMessageRouter::getTaskMessageRouter();
38-
39-
if($this->options["store"]["type"] == "mysql"){
40-
$this->taskStore = new MySQLTaskStore($this);
41-
} else if($this->options["store"]["type"] == "postgres"){
42-
$this->taskStore = new PostgresTaskStore($this);
43-
} else {
44-
throw new StoreNotImplementedException("Invalid Store choice.");
45-
}
46-
}
47-
48-
/**
49-
* @param $name
50-
* @return mixed
51-
* @throws UndefinedSettingsException
52-
*/
53-
public function getSetting($name){
54-
if(isset($this->options[$name])){
55-
return $this->options[$name];
56-
} else if(isset($this->defaultOptions[$name])) {
57-
return $this->defaultOptions[$name];
58-
} else {
59-
throw new UndefinedSettingsException("Option $name is not defined. ");
60-
}
61-
}
62-
63-
/**
64-
* @param $taskID
65-
* @return mixed|HTTPClientTask
66-
* @throws InvalidApiException
67-
* @throws UndefinedSettingsException
68-
*/
69-
public function getTaskByID($taskID){
70-
$taskStore = $this->getTaskStore();
71-
72-
$task = $taskStore->getTask($taskID);
73-
$task["taskID"] = $taskID;
74-
75-
$task = $this->newTask($task);
16+
private $options;
17+
18+
private $taskMessageRouter;
19+
private $taskStore;
20+
21+
private $defaultOptions = [
22+
"api" => "httpclient",
23+
"store" => [
24+
"type" => "mysql",
25+
"uri" => "mysql://root:toor@localhost/dbname"
26+
]
27+
];
28+
29+
/**
30+
* Client constructor.
31+
* @param $options
32+
* @throws StoreNotImplementedException
33+
*/
34+
public function __construct($options) {
35+
$this->options = $options;
36+
37+
$this->taskMessageRouter = TaskMessageRouter::getTaskMessageRouter();
38+
39+
if($this->options["store"]["type"] == "mysql"){
40+
$this->taskStore = new MySQLTaskStore($this);
41+
} else if($this->options["store"]["type"] == "postgres"){
42+
$this->taskStore = new PostgresTaskStore($this);
43+
} else {
44+
throw new StoreNotImplementedException("Invalid Store choice.");
45+
}
46+
}
47+
48+
/**
49+
* @param $name
50+
* @return mixed
51+
* @throws UndefinedSettingsException
52+
*/
53+
54+
public function getSetting($name) {
55+
if(isset($this->options[$name])){
56+
return $this->options[$name];
57+
} else if(isset($this->defaultOptions[$name])) {
58+
return $this->defaultOptions[$name];
59+
} else {
60+
throw new UndefinedSettingsException("Option $name is not defined. ");
61+
}
62+
}
63+
64+
/**
65+
* @param $taskID
66+
* @return mixed|HTTPClientTask
67+
* @throws InvalidApiException
68+
* @throws UndefinedSettingsException
69+
*/
70+
71+
public function getTaskByID($taskID) {
72+
$taskStore = $this->getTaskStore();
73+
74+
$task = $taskStore->getTask($taskID);
75+
$task["taskID"] = $taskID;
76+
77+
$task = $this->newTask($task);
7678

7779
return $task;
78-
}
79-
80-
/**
81-
* @return TaskMessageRouter
82-
*/
83-
public function getTaskMessageRouter(){
84-
return $this->taskMessageRouter;
85-
}
86-
87-
/**
88-
* @return PostgresTaskStore
89-
*/
90-
public function getTaskStore(){
91-
return $this->taskStore;
92-
}
93-
94-
/**
95-
* @param $name
96-
* @return HTTPClientTask
97-
* @throws InvalidApiException
98-
* @throws UndefinedSettingsException
99-
*/
100-
public function newTask($name){
101-
if($this->getSetting("api") == "httpclient"){
102-
return new HTTPClientTask($this, $name);
103-
} else {
104-
throw new InvalidApiException("Invalid API choice. ");
105-
}
106-
}
80+
}
81+
82+
/**
83+
* @return TaskMessageRouter
84+
*/
85+
86+
public function getTaskMessageRouter() {
87+
return $this->taskMessageRouter;
88+
}
89+
90+
/**
91+
* @return PostgresTaskStore
92+
*/
93+
94+
public function getTaskStore() {
95+
return $this->taskStore;
96+
}
97+
98+
/**
99+
* @param $name
100+
* @return HTTPClientTask
101+
* @throws InvalidApiException
102+
* @throws UndefinedSettingsException
103+
*/
104+
105+
public function newTask($name) {
106+
if($this->getSetting("api") == "httpclient"){
107+
return new HTTPClientTask($this, $name);
108+
} else {
109+
throw new InvalidApiException("Invalid API choice. ");
110+
}
111+
}
107112
}
108113

src/WorkerJS/PHPClient/HTTPClientTask.php

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,72 +13,72 @@
1313

1414
class HTTPClientTask extends Task{
1515

16-
private $client;
17-
private $name;
18-
19-
20-
public function __construct($client, $name)
21-
{
22-
parent::__construct($client, $name);
23-
$this->client = $client;
24-
$this->name = $name;
25-
}
26-
27-
/**
28-
* @return mixed
29-
* @throws RequestException
30-
* @throws ResponseException
31-
*/
32-
public function sendTask(){
33-
$url = $this->client->getSetting("api_base")."/task";
34-
$payload = $this->getTask();
35-
36-
$taskResponse = $this->sendRequest($url, $payload);
37-
38-
$this->client->getTaskStore()->setTask($taskResponse["taskID"], $this);
39-
return $taskResponse;
40-
}
41-
42-
/**
43-
* @param $payload
44-
* @return mixed
45-
* @throws RequestException
46-
* @throws ResponseException
47-
*/
48-
public function sendMessage($payload){
49-
$url = $this->client->getSetting("api_base")."/message";
50-
$request = [
51-
"taskID" => $this->task["taskID"],
52-
"message" => $payload,
53-
];
54-
55-
return $this->sendRequest($url, $request);
56-
}
57-
58-
59-
public function preProcess(){
60-
$this->task->webhook = $this->client->getSetting("webhook");
61-
}
62-
63-
private function sendRequest($url, $payload){
64-
$ch = curl_init();
65-
66-
curl_setopt($ch, CURLOPT_URL, $url);
67-
curl_setopt($ch, CURLOPT_POST, true);
68-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
69-
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
70-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
71-
72-
$result = curl_exec($ch);
73-
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
74-
75-
if($error = curl_error($ch)){
76-
throw new RequestException($error);
77-
}else if($code !== 200) {
78-
throw new ResponseException($result);
79-
} else {
80-
return json_decode($result, true);
81-
}
82-
}
16+
private $client;
17+
private $name;
18+
19+
20+
public function __construct($client, $name) {
21+
parent::__construct($client, $name);
22+
$this->client = $client;
23+
$this->name = $name;
24+
}
25+
26+
/**
27+
* @return mixed
28+
* @throws RequestException
29+
* @throws ResponseException
30+
*/
31+
32+
public function sendTask() {
33+
$url = $this->client->getSetting("api_base")."/task";
34+
$payload = $this->getTask();
35+
36+
$taskResponse = $this->sendRequest($url, $payload);
37+
38+
$this->client->getTaskStore()->setTask($taskResponse["taskID"], $this);
39+
return $taskResponse;
40+
}
41+
42+
/**
43+
* @param $payload
44+
* @return mixed
45+
* @throws RequestException
46+
* @throws ResponseException
47+
*/
48+
49+
public function sendMessage($payload) {
50+
$url = $this->client->getSetting("api_base")."/message";
51+
$request = [
52+
"taskID" => $this->task["taskID"],
53+
"message" => $payload,
54+
];
55+
56+
return $this->sendRequest($url, $request);
57+
}
58+
59+
public function preProcess() {
60+
$this->task->webhook = $this->client->getSetting("webhook");
61+
}
62+
63+
private function sendRequest($url, $payload) {
64+
$ch = curl_init();
65+
66+
curl_setopt($ch, CURLOPT_URL, $url);
67+
curl_setopt($ch, CURLOPT_POST, true);
68+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
69+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
70+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
71+
72+
$result = curl_exec($ch);
73+
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
74+
75+
if($error = curl_error($ch)){
76+
throw new RequestException($error);
77+
}else if($code !== 200) {
78+
throw new ResponseException($result);
79+
} else {
80+
return json_decode($result, true);
81+
}
82+
}
8383
}
8484

src/WorkerJS/PHPClient/MySQLTaskStore.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@
1111
use WorkerJS\PHPClient\exceptions\TaskNotFoundException;
1212

1313
class MySQLTaskStore extends TaskStore {
14-
private $connection;
14+
private $connection;
1515

16-
public function __construct($client){
17-
parent::__construct($client);
16+
public function __construct($client) {
17+
parent::__construct($client);
1818

19-
$connection = parse_url($this->client->getSetting("store")["uri"]);
19+
$connection = parse_url($this->client->getSetting("store")["uri"]);
2020

21-
$this->connection = mysqli_connect($connection["host"], $connection["user"], $connection["pass"], trim($connection["path"], "/"));
22-
}
21+
$this->connection = mysqli_connect($connection["host"], $connection["user"], $connection["pass"], trim($connection["path"], "/"));
22+
}
2323

2424

25-
public function getTask($taskID){
26-
$result = mysqli_query($this->connection, "SELECT `task` FROM `tasks` WHERE `taskID` = '".mysqli_real_escape_string($this->connection, $taskID)."'");
25+
public function getTask($taskID) {
26+
$result = mysqli_query($this->connection, "SELECT `task` FROM `tasks` WHERE `taskID` = '".mysqli_real_escape_string($this->connection, $taskID)."'");
2727

28-
if(mysqli_num_rows($result) === 0){
29-
throw new TaskNotFoundException("Task $taskID not found.");
30-
} else {
31-
$task = mysqli_fetch_assoc($result)["task"];
32-
return json_decode($task);
33-
}
34-
}
28+
if(mysqli_num_rows($result) === 0){
29+
throw new TaskNotFoundException("Task $taskID not found.");
30+
} else {
31+
$task = mysqli_fetch_assoc($result)["task"];
32+
return json_decode($task);
33+
}
34+
}
3535

3636

37-
public function setTask($taskID, Task $task){
38-
mysqli_query($this->connection, "INSERT INTO `tasks` (`taskID`, `task`) VALUES ('".mysqli_real_escape_string($this->connection, $taskID)."', '".mysqli_real_escape_string($this->connection, json_encode($task->getTask()))."')");
39-
}
37+
public function setTask($taskID, Task $task) {
38+
mysqli_query($this->connection, "INSERT INTO `tasks` (`taskID`, `task`) VALUES ('".mysqli_real_escape_string($this->connection, $taskID)."', '".mysqli_real_escape_string($this->connection, json_encode($task->getTask()))."')");
39+
}
4040
}

0 commit comments

Comments
 (0)