Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1.     public function createCommand(Request $req, Response $resp, $args)
  2.     {
  3.         header("Access-Control-Allow-Origin: *");
  4.         if ($req->getAttribute('has_errors')) {
  5.             $errors = $req->getAttribute('errors');
  6.             var_dump($errors);
  7.             foreach ($errors as $key => $listerrorAttribute) {
  8.                 echo "<strong>" . $key . " : </strong><br/>";
  9.                 //echo "<br/>";
  10.                 foreach ($listerrorAttribute as $error) {
  11.                     echo $error;
  12.                     echo "<br/>";
  13.                 }
  14.             }
  15.         } else {
  16.             $resp = $resp->withHeader('Content-Type', 'application/json');
  17.             $req_body = $req->getBody()->getContents();
  18.  
  19.  
  20.             if (Json::isJson($req_body)) {
  21.                 $body = json_decode($req_body, true);
  22.                 $resp = $resp->withStatus(500);
  23.                 try {
  24.                     $uuid = Uuid::uuid1();
  25.                 } catch (\Exception $e) {
  26.                     echo $e;
  27.                 }
  28.                 $commande = new Commande();
  29.                 $commande->id = $uuid->toString();
  30.                 $commande->nom = filter_var($body["nom"], FILTER_SANITIZE_STRING);
  31.                 $commande->mail = filter_var($body["mail"], FILTER_SANITIZE_STRING);
  32.                 $commande->token = bin2hex(openssl_random_pseudo_bytes(32));
  33.                 $commande->montant = 0;
  34.                 $commande->livraison = $body["livraison"]["date"] . " " . $body["livraison"]["heure"];
  35.  
  36.                 if (isset($body["client_id"])) {
  37.                     $client = Client::find($body["client_id"]);
  38.                     if ($client) {
  39.                         $token = explode(" ", $req->getHeader("Authorization")[0])[1];
  40.                         $tokenDecoded = JWT::decode($token, "lul", array('HS512'));
  41.  
  42.                         if ($client->id == $tokenDecoded->id)
  43.                             $commande->client_id = $body["client_id"];
  44.                     }
  45.                 }
  46.                 $total = 0;
  47.                 foreach ($body["items"] as $item) {
  48.                     $total += $commande->addItem($item);
  49.                     if (isset($body["client_id"])) {
  50.                         $client->cumul_achats += $total;
  51.                         $client->save();
  52.                     }
  53.                 }
  54.                 $commande->save();
  55.  
  56.                 $resp->getBody()->write(Json::resource("commande", $commande->toArray()));
  57.  
  58.                 $resp = $resp->withHeader("Location", "http://api.commande.local:19080/commands/" . $uuid->toString());
  59.                 $resp = $resp->withStatus(201);
  60.             } else {
  61.                 $resp->getBody()->write(Json::error(500, "merci de transmettre du JSON valide"));
  62.             }
  63.         }
  64.         return $resp->withHeader('Access-Control-Allow-Origin', 'http://api.commande.local');
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement