-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.php
145 lines (132 loc) · 4.17 KB
/
Command.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
139
140
141
142
143
144
145
<?php namespace Acme;
use Exception;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class Command
* @package Acme
*
* This is the default command class that we will be using
* to parse through the files and call the sub-commands
* based on the input file.
*/
class Command extends SymfonyCommand {
public function __construct()
{
parent::__construct();
}
public function configure()
{
$this->setName('command');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* Simple execute command that reads the input file,
* runs commands, and prints the summary to console
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$this->readInputFileAndRunCommands($input, $output);
$this->printSummaryToConsole($output);
}
/**
* @param InputInterface $input
* This is the meat and potatoes that parses the input file
* and runs the commands line-by-line
*/
private function readInputFileAndRunCommands(InputInterface $input, OutputInterface $output){
$file_name = $input->getFirstArgument();
$handle = null;
try {
if($file_name){
$handle = fopen($file_name, 'r');
} else {
$handle = fopen('php://stdin', 'r');
}
while (($line = fgets($handle)) !== false) {
$command = explode(" ", $line)[0];
switch ($command) {
case "Add":
$this->callAddUserCommand($line, $output);
break;
case "Remove":
$this->callRemoveUserCommand($line, $output);
break;
case "Charge":
$this->callChargeUserCommand($line, $output);
break;
case "Credit":
$this->callCreditUserCommand($line, $output);
break;
}
}
} catch (Exception $e){
$output->writeln($e->getMessage());
}
}
private function printSummaryToConsole(OutputInterface $output){
//fetch all users
$users = DatabaseConnection::get()->fetchAllUsers();
foreach($users as $user){
if($user['is_valid']){
$output->writeln($user['Name'] . ": $" . $user['Balance']);
} else {
$output->writeln($user['Name'] . ": error");
}
}
DatabaseConnection::get()->truncate();
}
private function callAddUserCommand($line, OutputInterface $output){
$command = $this->getApplication()->find('add_user');
$line = explode(" ", $line);
$arguments = array('name' => $line[1],
'cardNumber' => $line[2],
'limit' => $line[3]
);
$input = new ArrayInput($arguments);
$command->run($input, $output);
}
private function callRemoveUserCommand($line, OutputInterface $output)
{
$command = $this->getApplication()->find('remove_user');
$line = explode(" ", $line);
$arguments = array('name' => $line[1]);
$input = new ArrayInput($arguments);
$command->run($input, $output);
}
private function callCreditUserCommand($line, OutputInterface $output) {
$command = $this->getApplication()->find('credit_user');
$line = explode(" ", $line);
$arguments = array('name' => $line[1], 'amount' => $line[2]);
$input = new ArrayInput($arguments);
$command->run($input, $output);
}
private function callChargeUserCommand($line, OutputInterface $output){
$command = $this->getApplication()->find('charge_user');
$line = explode(" ", $line);
$arguments = array('name' => $line[1], 'amount' => $line[2]);
$input = new ArrayInput($arguments);
$command->run($input,$output);
}
protected function changeUserBalance($name, $amount, $type)
{
$amount = str_replace("$", "", $amount);
$user = DatabaseConnection::get()->fetchUserByName($name);
if((int)$user['is_valid']){
if($type == 'credit_user'){
$user['Balance'] -= $amount;
} elseif($type == 'charge_user'){
$user['Balance'] += $amount;
}
} else {
return;
}
if($user['Balance'] < $user['Limit']) {
DatabaseConnection::get()->updateBalanceForUser($user);
}
}
}