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.

74 lines
1.9 KiB

  1. <?php
  2. require_once('vendor/autoload.php');
  3. echo 'Welcome to the simple REPL for handling stock of items'.PHP_EOL;
  4. echo PHP_EOL;
  5. echo 'Selling usage: S<amount> [item-name]'.PHP_EOL;
  6. echo 'Inventory usage: I [item-name]'.PHP_EOL;
  7. echo 'Refilling usage: R<amount> [item-name]'.PHP_EOL;
  8. echo 'Quitting: q'.PHP_EOL;
  9. echo PHP_EOL;
  10. echo 'Default item-name is "box".'.PHP_EOL;
  11. echo PHP_EOL;
  12. // Default item name
  13. $defaultItem = 'box';
  14. // Set up http client
  15. $httpClient = new \GuzzleHttp\Client([
  16. 'base_uri' => $argv[1],
  17. 'http_errors' => false, // Disables obnoxious exceptions for 404's (and all others but well...)
  18. ]);
  19. while (true) {
  20. echo PHP_EOL.'> ';
  21. // Read user input
  22. $line = trim(strtolower(fgets(STDIN)));
  23. // Handle quit
  24. if ($line === 'q') {
  25. break;
  26. }
  27. // Parse input
  28. preg_match('/^([sir])(\d+)?\s?(.+)?$/', $line, $matches);
  29. if (empty($matches)) {
  30. echo 'invalid input'.PHP_EOL;
  31. continue;
  32. }
  33. // Read out fields
  34. $action = $matches[1];
  35. $amount = $matches[2] ?? 0;
  36. $item = $matches[3] ?? $defaultItem;
  37. switch ($action) {
  38. case 's':
  39. // Sell case
  40. $response = $httpClient->delete('/inventory/'.$item.'/'.$amount);
  41. $result = json_decode($response->getBody());
  42. echo $result->status.', available amount: '.($result->available_amount ?? 0).PHP_EOL;
  43. break;
  44. case 'i':
  45. // Inventory case
  46. $response = $httpClient->get('/inventory/'.$item);
  47. $result = json_decode($response->getBody());
  48. echo $result->status.', current stock: '.($result->stock ?? 0).PHP_EOL;
  49. break;
  50. case 'r':
  51. // Refill case
  52. $response = $httpClient->put('/inventory/'.$item.'/'.$amount);
  53. $result = json_decode($response->getBody());
  54. echo $result->status.PHP_EOL;
  55. break;
  56. }
  57. }