Examples of code I've written in PHP, Javascript, SCSS, etc.
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.
 
 
 
 
 
 

170 line
4.3 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Models\Supplier;
  7. use App\Models\Order;
  8. class Supply extends Controller
  9. {
  10. // Accepts a site name, service id, and integer/array of supplier rows to
  11. // create
  12. public static function fetch($site) {
  13. return Http::post(config("services.$site.link"), ['key' =>
  14. config("services.$site.key"), 'action' => 'services']);
  15. }
  16. public static function smmkings($service_id, $id) {
  17. $services = Http::post(config("services.smmkings.link"), ['key' =>
  18. config("services.smmkings.key"), 'action' => 'services'])->json();
  19. foreach ($services as $service) {
  20. if ($service['service'] == $id){
  21. $s = new Supplier;
  22. $s->service_id = $service_id;
  23. $s->supplier = 'smmkings';
  24. $s->supplier_id = (int) $service['service'];
  25. $s->supplier_name = $service['name'];
  26. $s->min = (int) $service['min'];
  27. $s->max = (int) $service['max'];
  28. $s->cost = $service['rate'] * 100;
  29. $s->save();
  30. return $s;
  31. }
  32. }
  33. }
  34. public static function smmworld($service_id, $id) {
  35. $services = Http::post(config("services.smmworld.link"), ['key' =>
  36. config("services.smmworld.key"), 'action' => 'services']);
  37. $services = $services['services'];
  38. foreach ($services as $service) {
  39. if ($service['id'] == $id){
  40. $s = new Supplier;
  41. $s->service_id = $service_id;
  42. $s->supplier = 'smmworld';
  43. $s->supplier_id = $service['id'];
  44. $s->supplier_name = $service['name'];
  45. $s->min = $service['min'];
  46. $s->max = $service['max'];
  47. $s->cost = $service['price_per_k'] * 100;
  48. $s->save();
  49. return $s;
  50. }
  51. }
  52. }
  53. //Fulfills an order with only the primary supplier of it's service
  54. public static function fulfill($order) {
  55. if (gettype($order) == 'integer') {
  56. $order = Order::find($order);
  57. }
  58. return Supply::orderPrimary($order);
  59. }
  60. //Fulfills an order with the first available supplier it finds. It returns
  61. //false if none could be found and the refreshed order model otherwise.
  62. public static function fulfillWithAny($order) {
  63. if (gettype($order) == 'integer') {
  64. $order = Order::find($order);
  65. }
  66. if (Supply::orderPrimary($order)) {
  67. return $order->refresh();
  68. };
  69. $suppliers = $order->service->suppliers;
  70. foreach ($suppliers as $s) {
  71. switch ($s->supplier) {
  72. case 'smmkings':
  73. if (Supply::orderSmmkings($order, $s)){
  74. return $order->refresh();
  75. }
  76. break;
  77. case 'smmworld':
  78. if (Supply::orderSmmkings($order, $s)){
  79. return $order->refresh();
  80. }
  81. break;
  82. }
  83. }
  84. return false;
  85. }
  86. //Fulfills an order with only the primary supplier of it's service
  87. public static function orderPrimary($order) {
  88. if (gettype($order) == 'integer') {
  89. $order = Order::find($order);
  90. }
  91. $primary = $order->service->primary;
  92. switch ($primary->supplier) {
  93. case 'smmkings':
  94. return Supply::orderSmmkings($order, $primary);
  95. break;
  96. case 'smmworld':
  97. return Supply::orderSmmworld($order, $primary);
  98. break;
  99. }
  100. }
  101. //Should check that cost is not greater than price and store order
  102. //information if completed. return false/NULL if an error occured
  103. public static function orderSmmkings($order, $supplier) {
  104. if ($supplier->supplier != 'smmkings') {
  105. return false;
  106. }
  107. //Check that the order is not already pending. Probably by calling
  108. //another function.
  109. //Check supplier cost here to make sure the request is a good idea
  110. $response = Http::post(config("services.smmkings.link"), ['key' =>
  111. config("services.smmkings.key"), 'action' => 'add',
  112. 'service' => $supplier->supplier_id, 'link' =>
  113. $order->url, 'quantity' => $order->quantity])->json();
  114. //This should log the error for later reporting
  115. if (array_key_exists('error', $response)){
  116. return;
  117. }
  118. //store order information here
  119. $status = Http::post(config("services.smmkings.link"), ['key' =>
  120. config("services.smmkings.key"), 'action' => 'status',
  121. 'order' => $response['order']])->json();
  122. return $response;
  123. }
  124. public static function orderSmmworld($order, $supplier) {
  125. if ($supplier->supplier != 'smmworld') {
  126. return false;
  127. }
  128. $response = Http::post(config("services.smmworld.link"), ['key' =>
  129. config("services.smmworld.key"), 'action' => 'add',
  130. 'service' => $supplier->supplier_id, 'link' => $order->url, 'quantity' =>
  131. $order->quantity])->json();
  132. return $response;
  133. }
  134. }