1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<?php
declare(strict_types=1);
namespace AugustOffensive\Controller;
use AugustOffensive\Model;
/**
* Static controller class for interfacing between the view and the model.
*/
class Controller
{
/**
* Initiates connection with the database.
*
* While errors are ideally handled by the controller, this instantiation
* will likely throw a \PDOException which should be handled by the front-
* end due to this being a fatal error and generally unrecoverable.
*
* @return Model\Connection
*/
public static function initiateConnection (): Model\Connection
{
return new Model\Connection();
}
/**
* Creates and returns a Query object.
*
* If the creation results in an error, a different query object is
* returned with the error message.
*
* @param array $path The array that holds the original request structure.
* @param string $request The request method made to the server.
* @param array $content The content object sent by the request.
*
* @return Model\Query
*/
public static function createQuery (
array $path,
string $request,
array $content
): Model\Query {
try {
return new Model\Query($path, $request, $content);
} catch (\Exception $err) {
return new Model\Query(array(), "", array("ERROR" => $err->getMessage()));
}
}
/**
* Creates and returns a Result object.
*
* @param string $resultType The type of result to send back to the client.
* @param array $result The result object to send back to the client.
*
* @return Model\Result
*/
public static function createResult (
string $resultType,
array $result
): Model\Result {
try {
return new Model\Result($resultType, $result);
} catch (\Exception $err) {
return new Model\Result("ERROR", array($err->getMessage()));
}
}
/**
* Obtain the error result based on the exception that was thrown.
*
* @param \Exception $err the error that was thrown.
*
* @return Model\Result
*/
public static function errorResult (\Exception $err): Model\Result
{
$errorType = "";
// Juggle error: objective is to sort error type
try {
throw $err;
} catch (\PDOException $e) {
$errorType = "DATABASE_ERROR";
} catch (\Exception $e) {
$errorType = "ERROR";
}
return new Model\Result($errorType, array("error" => $err->getMessage()));
}
}
|