forked from adebayohountondji/kata-SOLID-principles-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.php
116 lines (98 loc) · 3.44 KB
/
cli.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
<?php
require_once __DIR__ . "/vendor/autoload.php";
use App\Orders\NewOrderData;
use App\Orders\Order;
use App\Orders\OrderAlreadyExistsError;
use App\Orders\OrderNotFoundError;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
if (file_exists(__DIR__ . "/.env")) {
$_ENV = (Dotenv\Dotenv::createImmutable(__DIR__))
->load();
}
$application = new Application(name: "Kata SOLID principles", version: "1.0.0");
$application->register("create-order")
->addArgument("id", InputArgument::REQUIRED, "Must be an unsigned integer.")
->addOption(
"value-added-tax",
"vat",
InputOption::VALUE_OPTIONAL,
"Must be between 0.00 and 1.00.",
Order::DEFAULT_VAT_RATE
)
->setDescription("Create a new order with an ID and a VAT rate.")
->setCode(function (InputInterface $input, OutputInterface $output): int {
try {
Order::new(
new NewOrderData(
id: $input->getArgument("id"),
vat_rate: $input->getOption("value-added-tax"),
)
);
} catch (OrderAlreadyExistsError $e) {
$output->writeln($e->getMessage());
return Command::FAILURE;
}
$output->writeln(
"Successfully created order with ID {$input->getArgument("id")}.",
);
return Command::SUCCESS;
});
$application->register("display-order")
->addArgument("id", InputArgument::REQUIRED, "Must be an unsigned integer.")
->setDescription("Display an order by ID.")
->setCode(function (InputInterface $input, OutputInterface $output): int {
try {
$order = Order::findById($input->getArgument("id"));
} catch (OrderNotFoundError $e) {
$output->writeln($e->getMessage());
return Command::FAILURE;
}
$output->writeln($order);
return Command::SUCCESS;
});
$application->register("add-item-to-order")
->addArgument("id", InputArgument::REQUIRED, "Must be an unsigned integer.")
->addOption(
"item-name",
null,
InputOption::VALUE_REQUIRED,
"Must be a string."
)
->addOption(
"item-price",
null,
InputOption::VALUE_REQUIRED,
"Must be a float."
)
->addOption(
"item-quantity",
null,
InputOption::VALUE_REQUIRED,
"Must be an integer."
)
->setDescription("Add item to order by ID.")
->setCode(function (InputInterface $input, OutputInterface $output): int {
try {
$order = Order::findById($input->getArgument("id"));
} catch (OrderNotFoundError $e) {
$output->writeln($e->getMessage());
return Command::FAILURE;
}
$order->addItem(
new \App\Orders\ItemData(
name: $input->getOption("item-name"),
price: (float)$input->getOption("item-price"),
quantity: (int)$input->getOption("item-quantity"),
)
);
$output->writeln(
"Item '{$input->getOption("item-name")}' successfully added to order {$input->getArgument("id")}."
);
return Command::SUCCESS;
});
$application->run();