Browse Source

refactoring

master
Simon Vieille 3 weeks ago
parent
commit
a7053dfef7
Signed by: Simon Vieille <simon@deblan.fr> GPG Key ID: 03383D15A1D31745
6 changed files with 126 additions and 79 deletions
  1. 4
    1
      composer.json
  2. 57
    0
      src/actions.php
  3. 37
    0
      src/parsers.php
  4. 0
    0
      src/routing.php
  5. 26
    0
      src/validators.php
  6. 2
    78
      web/index.php

+ 4
- 1
composer.json View File

@@ -1,7 +1,10 @@
1 1
 {
2 2
     "autoload": {
3 3
         "files": [
4
-            "src/functions/simple-router.php"
4
+            "src/routing.php",
5
+            "src/validators.php",
6
+            "src/parsers.php",
7
+            "src/actions.php"
5 8
         ]
6 9
     },
7 10
     "require": {

+ 57
- 0
src/actions.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+
3
+class Action
4
+{
5
+    /**
6
+     * Home action.
7
+     *
8
+     * @return string
9
+     */
10
+    public function home():string
11
+    {
12
+        return response()
13
+            ->httpCode(200)
14
+            ->json([]);
15
+    }
16
+
17
+    /**
18
+     * Parser action.
19
+     *
20
+     * @param string $parser
21
+     *
22
+     * @return string
23
+     */
24
+    public function parse(string $parser):string
25
+    {
26
+        $url = input()->get('url')->value;
27
+
28
+        if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
29
+            return response()
30
+                ->httpCode(400)
31
+                ->json(['error' => 'Invalid URL']);
32
+        }
33
+
34
+        $parsers = [
35
+            'graby' => 'graby',
36
+            'opengraph' => 'opengraph',
37
+        ];
38
+
39
+        if (!isset($parsers[$parser])) {
40
+            return response()
41
+                ->httpCode(400)
42
+                ->json(['error' => 'Invalid parser']);
43
+        }
44
+
45
+        try {
46
+            $result = call_user_func_array($parsers[$parser], [$url]);
47
+
48
+            return response()
49
+                ->httpCode(200)
50
+                ->json($result);
51
+        } catch (\Exception $e) {
52
+            return response()
53
+                ->httpCode(500)
54
+                ->json(['error' => $e->getMessage()]);
55
+        }
56
+    }
57
+}

+ 37
- 0
src/parsers.php View File

@@ -0,0 +1,37 @@
1
+<?php
2
+
3
+use Graby\Graby;
4
+use Fusonic\OpenGraph\Consumer;
5
+
6
+/**
7
+ * Graby.
8
+ *
9
+ * @param string $url
10
+ * @return array
11
+ */
12
+function graby(string $url) : array
13
+{
14
+    $graby = new Graby();
15
+    $result = $graby->fetchContent($url);
16
+
17
+    if (isset($result['headers'])) {
18
+        unset($result['headers']);
19
+    }
20
+
21
+    return $result;
22
+}
23
+
24
+/**
25
+ * Opengraph
26
+ *
27
+ * @param string $url
28
+ *
29
+ * @return array
30
+ */
31
+function opengraph(string $url) : array
32
+{
33
+    $consumer = new Consumer();
34
+    $object = $consumer->loadUrl($url);
35
+
36
+    return json_decode(json_encode($object), true);
37
+}

src/functions/simple-router.php → src/routing.php View File


+ 26
- 0
src/validators.php View File

@@ -0,0 +1,26 @@
1
+<?php
2
+
3
+/**
4
+ * Validates an URL.
5
+ *
6
+ * @param string|null $url
7
+ * @param string|null $hostname
8
+ *
9
+ * @return bool
10
+ */
11
+function isValidUrl( ? string $url, ? string $hostname) : bool
12
+{
13
+    if ($url === null) {
14
+        return false;
15
+    }
16
+
17
+    if (trim($url) === '') {
18
+        return false;
19
+    }
20
+
21
+    if (filter_var($url, FILTER_VALIDATE_URL) === false) {
22
+        return false;
23
+    }
24
+
25
+    return parse_url($url)['host'] !== $hostname;
26
+}

+ 2
- 78
web/index.php View File

@@ -1,7 +1,5 @@
1 1
 <?php
2 2
 
3
-use Graby\Graby;
4
-use Fusonic\OpenGraph\Consumer;
5 3
 use Pecee\SimpleRouter\SimpleRouter;
6 4
 
7 5
 require __DIR__.'/../vendor/autoload.php';
@@ -9,80 +7,6 @@ require __DIR__.'/../vendor/autoload.php';
9 7
 $dotenv = Dotenv\Dotenv::create(__DIR__.'/../');
10 8
 $dotenv->load();
11 9
 
12
-function isValidUrl( ? string $url, ? string $hostname) : bool
13
-{
14
-    if ($url === null) {
15
-        return false;
16
-    }
17
-
18
-    if (trim($url) === '') {
19
-        return false;
20
-    }
21
-
22
-    if (filter_var($url, FILTER_VALIDATE_URL) === false) {
23
-        return false;
24
-    }
25
-
26
-    return parse_url($url)['host'] !== $hostname;
27
-}
28
-
29
-function graby(string $url) : array
30
-{
31
-    $graby = new Graby();
32
-    $result = $graby->fetchContent($url);
33
-
34
-    if (isset($result['headers'])) {
35
-        unset($result['headers']);
36
-    }
37
-
38
-    return $result;
39
-}
40
-
41
-function opengraph(string $url) : array
42
-{
43
-    $consumer = new Consumer();
44
-    $object = $consumer->loadUrl($url);
45
-
46
-    return json_decode(json_encode($object), true);
47
-}
48
-
49
-SimpleRouter::get('/', function() {
50
-    return response()
51
-        ->httpCode(200)
52
-        ->json([]);
53
-});
54
-
55
-SimpleRouter::get('/do/{parser}', function ($parser) {
56
-    $url = input()->get('url')->value;
57
-
58
-    if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
59
-        return response()
60
-            ->httpCode(400)
61
-            ->json(['error' => 'Invalid URL']);
62
-    }
63
-
64
-    $parsers = [
65
-        'graby' => 'graby',
66
-        'opengraph' => 'opengraph',
67
-    ];
68
-
69
-    if (!isset($parsers[$parser])) {
70
-        return response()
71
-            ->httpCode(400)
72
-            ->json(['error' => 'Invalid parser']);
73
-    }
74
-
75
-    try {
76
-        $result = call_user_func_array($parsers[$parser], [$url]);
77
-
78
-        return response()
79
-            ->httpCode(200)
80
-            ->json($result);
81
-    } catch (\Exception $e) {
82
-        return response()
83
-            ->httpCode(500)
84
-            ->json(['error' => $e->getMessage()]);
85
-    }
86
-});
87
-
10
+SimpleRouter::get('/', 'Action@home');
11
+SimpleRouter::get('/do/{parser}', 'Action@parse');
88 12
 SimpleRouter::start();

Loading…
Cancel
Save