<?php
namespace Incoviba\Controller\Ventas;

use Incoviba\Common\Implement\Exception\EmptyResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Repository;

class Comentarios
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Comentario $comentarioRepository): ResponseInterface
    {
        $output = ['total' => 0];
        try {
            $comentarios = $comentarioRepository->fetchAll();
            $output['comentarios'] = $comentarios;
            $output['total'] = count($comentarios);
        } catch (EmptyResult) {}
        $response->getBody()->write(json_encode($output));
        return $response->withHeader('Content-Type', 'application/json');
    }
}
