-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport.php
138 lines (123 loc) · 4.1 KB
/
report.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* Copyright ViksTech di Vittorio Domenico Padiglia.
* Se non hai pagato per l'uso o la modifica di questi sorgenti, hai il dovere di cancellarli
* Il possesso e l'uso, o la copia, di questo codice non consentito è punibile per legge.
*/
/**
* @var bool DEBUG if True enable Error Print
*/
define('DEBUG', true);
/**
* @var string CONTROLLER Controller absolute PATH
*/
define('CONTROLLER', __DIR__ . '/controller/');
/**
* @var string MODEL Modelabsolute PATH
*/
define('MODEL', __DIR__ . '/model/');
/**
* @var string CORE Core absolute PATH
*/
define('CORE', __DIR__ . '/core/');
/**
* @var string DATA Data absolute PATH
*/
define('DATA', __DIR__ . '/data/');
class app {
/**
* @var int $customerId will contain the customer ID passed by client args
*/
private $customerId;
/**
* @var int $customersController will be instance of Customers, the customers controller
*/
private $customersController;
public function __construct() {
}
/**
* Dispatch actions.
*
* @param string $action Name of the needed action.
*/
public function dispatch(string $action) {
// Init $customerId or exit
if ($this->initCustomerId() == null)
return null;
// Load Customers controller or exit
if (!$this->loadController('Customers'))
return null;
$this->customersController = new \controller\Customers();
// Load Customer Data By ID, Customer will be Null or \model\Customer object
$customer = $this->loadCustomerData();
// Get Customer Transactions
$transactions = $customer->getTransactions();
// Load Customers controller or exit
if (!$this->loadController('Converter'))
return null;
// Create a Converter Instance
$converter = new \controller\Converter();
foreach ($transactions as $k => $transaction) {
// Get the converted value
$transactions[$k]['valueConverted'] = $converter->convert($transaction['value']);
}
switch ($action) {
case 'plain':
default:
if (!count($transactions)) {
return "No transaction for that customer {$this->customerId}\n";
}
$txt = sprintf('There are %d transactions for Customer [%d] %s %s', count($transactions), $customer->getId(), $customer->getName(), $customer->getSurname()) . "\n";
foreach ($transactions as $transaction) {
$txt .= implode(' ', $transaction) . "\n";
}
return $txt;
break;
case 'json':
return json_encode($transactions);
break;
}
}
/**
* Load required Controller.
*
* @param string $controllerName Name of controller.
*
* @return bool will return true on Controller file loaded.
*/
public function loadController(string $controllerName) {
$file = CONTROLLER . "$controllerName/$controllerName.php";
// Check if file exists, otherwise create exception
if (!file_exists($file)) {
throw new Exception("Controller $controllerName not found in $file");
return;
}
// Require the controller file
return require_once $file;
}
/**
* Will Load customer data by $customerId value.
*
* @return model\Customer|null Returns null or the Customer Object.
*/
public function loadCustomerData() {
return $this->customersController->getCustomer($this->customerId);
}
/**
* Will Get Customer ID from PHP Cli ARGS.
*
* @return int|bool Returns and set the customer ID.
*/
public function initCustomerId() {
global $argv;
if (($this->customerId = $argv[1] ?? null) === null) {
throw new Exception('No ID provided');
return false;
}
if (!is_numeric($this->customerId)) {
throw new Exception('No Valid ID provided');
return false;
}
return $this->customerId;
}
}