瀏覽代碼

added Multiple and HttpRequest commands

tags/0.1.4^0
Robin Thoni 5 年之前
父節點
當前提交
8b89fb3fd0
共有 2 個文件被更改,包括 96 次插入0 次删除
  1. 58
    0
      app/Http/Business/Commands/HttpRequestCommand.php
  2. 38
    0
      app/Http/Business/Commands/MultipleCommand.php

+ 58
- 0
app/Http/Business/Commands/HttpRequestCommand.php 查看文件

@@ -0,0 +1,58 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 11/24/15
6
+ * Time: 12:15 AM
7
+ */
8
+
9
+namespace App\Http\Business\Commands;
10
+
11
+use GuzzleHttp\Client;
12
+use GuzzleHttp\Exception\ClientException;
13
+use GuzzleHttp\Exception\RequestException;
14
+use Luticate\Utils\LuLog;
15
+
16
+class HttpRequestCommand extends AbstractCommand
17
+{
18
+    /**
19
+     * @return bool
20
+     */
21
+    public function exec()
22
+    {
23
+        $data = $this->_command->getData();
24
+        $url = $data["Url"];
25
+        $method = $data["Method"];
26
+        $options = $data["Options"];
27
+
28
+        $client = new Client();
29
+
30
+        try {
31
+            $response = $client->request($method, $url, $options);
32
+        } catch (ClientException $e) {
33
+            $response = $e->getResponse();
34
+            if (!is_null($response)) {
35
+                LuLog::log($response->getBody());
36
+            }
37
+            LuLog::log($e);
38
+            abort(500, "Failed to execute request (client)");
39
+        } catch (RequestException $e) {
40
+            $response = $e->getResponse();
41
+            if (!is_null($response)) {
42
+                LuLog::log($response->getBody());
43
+            }
44
+            LuLog::log($e);
45
+            abort(500, "Failed to execute request (request)");
46
+        } catch (\Exception $e) {
47
+            LuLog::log($e);
48
+            abort(500, "Failed to execute request (unknown)");
49
+        }
50
+
51
+        if ($response->getStatusCode() != 200)
52
+        {
53
+            abort(500, "Failed to execute request (" . $response->getStatusCode() . ")");
54
+        }
55
+
56
+        return true;
57
+    }
58
+}

+ 38
- 0
app/Http/Business/Commands/MultipleCommand.php 查看文件

@@ -0,0 +1,38 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 11/24/15
6
+ * Time: 12:15 AM
7
+ */
8
+
9
+namespace App\Http\Business\Commands;
10
+
11
+use App\Http\DBO\CommandsDbo;
12
+
13
+class MultipleCommand extends AbstractCommand
14
+{
15
+    /**
16
+     * @return bool
17
+     */
18
+    public function exec()
19
+    {
20
+        $data = $this->_command->getData();
21
+
22
+        $commands = $data["Commands"];
23
+
24
+        foreach ($commands as $command) {
25
+            $type = $command["Type"];
26
+            $data = $command["Data"];
27
+            $class = "App\Http\Business\Commands\\" . $type . "Command";
28
+
29
+            $commandDbo = new CommandsDbo();
30
+            $commandDbo->setData($data);
31
+
32
+            $cmd = new $class($commandDbo);
33
+            $cmd->exec();
34
+        }
35
+
36
+        return true;
37
+    }
38
+}

Loading…
取消
儲存