Browse code

Added README.md appinfo/info.xml appinfo/signature.json lib/Controller/AuthorApiController.php and the providers directory

DoubleBastionAdmin authored on 20/08/2022 16:33:00
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,98 @@
1
+<?php
2
+
3
+namespace Telnyx;
4
+
5
+use \Symfony\Component\Process\Process;
6
+
7
+class TelnyxMock
8
+{
9
+    protected static $process = null;
10
+    protected static $port = -1;
11
+
12
+    const PATH_SPEC = __DIR__ . '/openapi/spec3.json';
13
+    const PATH_FIXTURES = __DIR__ . '/openapi/fixtures3.json';
14
+
15
+    /**
16
+     * Starts a telnyx-mock process with custom OpenAPI spec and fixtures files, if they exist.
17
+     *
18
+     * @return bool true if a telnyx-mock process was started, false otherwise.
19
+     */
20
+    public static function start()
21
+    {
22
+        if (!file_exists(self::PATH_SPEC)) {
23
+            return false;
24
+        }
25
+
26
+        if (!is_null(static::$process) && static::$process->isRunning()) {
27
+            echo "telnyx-mock already running on port " . static::$port . "\n";
28
+            return true;
29
+        }
30
+
31
+        static::$port = static::findAvailablePort();
32
+
33
+        echo "Starting telnyx-mock on port " . static::$port . "...\n";
34
+
35
+        static::$process = new Process(join(' ', [
36
+            'telnyx-mock',
37
+            '-http-port',
38
+            static::$port,
39
+            '-spec',
40
+            self::PATH_SPEC,
41
+            '-fixtures',
42
+            self::PATH_FIXTURES,
43
+        ]));
44
+        static::$process->start();
45
+        sleep(1);
46
+
47
+        if (static::$process->isRunning()) {
48
+            echo "Started telnyx-mock, PID = " . static::$process->getPid() . "\n";
49
+        } else {
50
+            die("telnyx-mock terminated early, exit code = " . static::$process->wait());
51
+        }
52
+
53
+        return true;
54
+    }
55
+
56
+    /**
57
+     * Stops the telnyx-mock process, if one was started. Otherwise do nothing.
58
+     */
59
+    public static function stop()
60
+    {
61
+        if (is_null(static::$process) || !static::$process->isRunning()) {
62
+            return;
63
+        }
64
+
65
+        echo "Stopping telnyx-mock...\n";
66
+        static::$process->stop(0, SIGTERM);
67
+        static::$process->wait();
68
+        static::$process = null;
69
+        static::$port = -1;
70
+        echo "Stopped telnyx-mock\n";
71
+    }
72
+
73
+    /**
74
+     * Returns the port number used by the telnyx-mock process.
75
+     *
76
+     * @return int the port number used by telnyx-mock, or -1 if no telnyx-mock process was started
77
+     */
78
+    public static function getPort()
79
+    {
80
+        return static::$port;
81
+    }
82
+
83
+    /**
84
+     * Finds a random available TCP port.
85
+     *
86
+     * @return int the port number
87
+     */
88
+    private static function findAvailablePort()
89
+    {
90
+        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
91
+        socket_bind($sock, "localhost", 0);
92
+        $addr = null;
93
+        $port = -1;
94
+        socket_getsockname($sock, $addr, $port);
95
+        socket_close($sock);
96
+        return $port;
97
+    }
98
+}