You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
1.7 KiB

<?php
require_once('vendor/autoload.php');
echo 'Welcome to the simple REPL for handling stock of items'.PHP_EOL;
echo PHP_EOL;
echo 'Selling usage: S<amount> [item-name]'.PHP_EOL;
echo 'Inventory usage: I [item-name]'.PHP_EOL;
echo 'Refilling usage: R<amount> [item-name]'.PHP_EOL;
echo 'Quitting: q'.PHP_EOL;
echo PHP_EOL;
echo 'Default item-name is "box".'.PHP_EOL;
echo PHP_EOL;
// Default item name
$defaultItem = 'box';
// Set up http client
$httpClient = new \GuzzleHttp\Client([
'base_uri' => $argv[1],
'http_errors' => false, // Disables obnoxious exceptions for 404's (and all others but well...)
]);
while (true) {
// Read user input
$line = trim(strtolower(fgets(STDIN)));
// Handle quit
if ($line === 'q') {
break;
}
// Parse input
preg_match('/^([sir])(\d+)?\s?(.+)?$/', $line, $matches);
if (empty($matches)) {
echo 'invalid input'.PHP_EOL;
continue;
}
// Read out fields
$action = $matches[1];
$amount = $matches[2] ?? 0;
$item = $matches[3] ?? $defaultItem;
switch ($action) {
case 's':
// Sell case
$response = $httpClient->delete('/inventory/'.$item.'/'.$amount);
$result = json_decode($response->getBody());
echo $result->status.', available amount: '.($result->available_amount ?? 0).PHP_EOL;
break;
case 'i':
// Inventory case
$response = $httpClient->get('/inventory/'.$item);
$result = json_decode($response->getBody());
echo $result->status.', current stock: '.($result->stock ?? 0).PHP_EOL;
break;
case 'r':
// Refill case
break;
}
}