PHP Classes

File: src/Api.php

Recommend this page to a friend!
  Classes of Rodrigo Faustino   Testes Unitarios   src/Api.php   Download  
File: src/Api.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Testes Unitarios
Show how to test classes using PHPUnit
Author: By
Last change:
Date: 2 months ago
Size: 1,246 bytes
 

Contents

Class file image Download
<?php
namespace App\Testes;

require
'../vendor/autoload.php';

use
App\Testes\User;
use
App\Testes\UserService;

header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];
$data = json_decode(file_get_contents('php://input'), true);

$user = new User();
$userService = new UserService($user);

$response = ['status' => 'error', 'message' => 'Requisição inválida'];

if (
$method === 'POST' && isset($data['action']) && isset($data['username']) && isset($data['password'])) {
   
$action = $data['action'];
   
    try {
        if (
$action === 'register') {
           
$userService->registrarUser($data['username'], $data['password']);
           
$response = ['status' => 'success', 'message' => 'Usuário registrado com sucesso'];
        } elseif (
$action === 'login') {
            if (
$userService->logarUser($data['username'], $data['password'])) {
               
$response = ['status' => 'success', 'message' => 'Login bem-sucedido'];
            } else {
               
$response = ['status' => 'error', 'message' => 'Credenciais incorretas'];
            }
        }
    } catch (\
Exception $e) {
       
$response = ['status' => 'error', 'message' => $e->getMessage()];
    }
}

echo
json_encode($response);