<?php
namespace Claromentis\People\Controller\Rest;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"] ?: array()) + array("_79bd87060" . "993ff622ef" . "212c1e8b6a" . "79a" => base64_decode("Rm9yYm" . "lkZGVu"), "_3efc5ba7bf51c" . "a70907f5e65ab6" . "02ec1" => base64_decode("SW50ZXJuYWwgU2VydmVyI" . "EVycm9y"), "_c894747360c" . "6ec5bd816953" . "0eaf9c473" => base64_decode("QmFkIFJlcXVlc" . "3Q="), "role_i" . "d" => base64_decode("cm9sZV9pZ" . "A=="), "_94c782c8180" . "766e1ff18709" . "75a93053d" => base64_decode("Um9sZSBub3" . "QgZm91bmQ="), "_a4a1018258adabb74e7b04836d" . "8d7e3b" => base64_decode("Tm90IEZv" . "dW5k"), "_3682611b4932733" . "eb97863fa0b39670" . "0" => base64_decode("RXJyb3Igd" . "XBkYXRpbm" . "cgcm9sZQ=" . "="), "_aabddb8e5540f596ece10005c" . "132c6b6" => base64_decode("RXJyb3Igc2V0dGluZyByb2xlIHV" . "zZXJz"), "_0ef58d1ad10b6ac762ed9c59" . "40c6feb7" => base64_decode("RXJyb3IgY3JlYXRpbmcgcm9sZQ=" . "="), "_25fb31f00b26bde87effd4759e" . "283d9f" => base64_decode("cm9sZV" . "9pZHM="), "_67b69c042916369a28c74a7" . "c8b278522" => base64_decode("RXJyb3I" . "gZGVsZX" . "Rpbmcgc" . "m9sZQ=="));
use Analogue\ORM\Exceptions\MappingException;


use Claromentis\Core\Http\JsonPrettyResponse;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\People\Exception\NotFoundException;
use Claromentis\People\Exception\PermissionException;
use Claromentis\People\Exception\ValidationException;
use Claromentis\People\Filter\RoleFilter;
use Claromentis\People\Filter\UserFilter;
use Claromentis\People\RestFormatter;
use Claromentis\People\UseCase\CreateRole;
use Claromentis\People\UseCase\DeleteRole;
use Claromentis\People\UseCase\GetRole;
use Claromentis\People\UseCase\GetRoles;
use Claromentis\People\UseCase\GetRoleUsers;
use Claromentis\People\UseCase\SetRoleUsers;
use Claromentis\People\UseCase\UpdateRole;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface as Request;
use RestExBadRequest;
use RestExError;
use RestExForbidden;
use RestExNotFound;
use UnexpectedValueException;






class RoleController extends AbstractController
{
    
    
    
    
    protected $formatter;
    
    
    
    
    protected $get_role;
    
    
    
    
    protected $get_roles;
    
    
    
    
    protected $create_role;
    
    
    
    
    protected $update_role;
    
    
    
    
    protected $delete_role;
    
    
    
    
    protected $get_role_users;
    
    
    
    
    protected $set_role_users;
    
    
    
    
    
    
    
    
    
    
    
    public function __construct(RestFormatter $formatter, GetRole $get_role, GetRoles $get_roles, CreateRole $create_role, UpdateRole $update_role, DeleteRole $delete_role, GetRoleUsers $get_role_users, SetRoleUsers $set_role_users)
    {
        $this->formatter = $formatter;
        $this->get_role = $get_role;
        $this->get_roles = $get_roles;
        $this->create_role = $create_role;
        $this->update_role = $update_role;
        $this->delete_role = $delete_role;
        $this->get_role_users = $get_role_users;
        $this->set_role_users = $set_role_users;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function GetOne(SecurityContext $context, Request $request, $role_id)
    {
        $use_case = $this->get_role;
        try {
            $_03e9164d0a343b742df4793268b80767 = new RoleFilter($context);
            $_03e9164d0a343b742df4793268b80767->Fill($this->formatter->GetRequestParams($request, $_03e9164d0a343b742df4793268b80767->GetDefaults()));
            $_d1071a032215f0eb5c4a8ef175f7049d = $use_case->GetRole($context, $role_id, $_03e9164d0a343b742df4793268b80767);
            return new JsonPrettyResponse($this->formatter->FormatRole($_d1071a032215f0eb5c4a8ef175f7049d, $_03e9164d0a343b742df4793268b80767));
        } catch (InvalidArgumentException|ValidationException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_c894747360c6ec5bd8169530eaf9c" . "473"], $exception);
        } catch (NotFoundException $exception) {
            throw new RestExNotFound($exception->getMessage(), $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_a4a1018258adabb" . "74e7b04836d8d7e3" . "b"], $exception);
        } catch (PermissionException $exception) {
            throw new RestExForbidden($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_79bd870609" . "93ff622ef21" . "2c1e8b6a79a"], $exception);
        }
    }
    
    
    
    
    
    
    
    
    
    public function GetList(SecurityContext $context, Request $request)
    {
        try {
            $_03e9164d0a343b742df4793268b80767 = new RoleFilter($context);
            $_03e9164d0a343b742df4793268b80767->Fill($this->formatter->GetRequestParams($request, $_03e9164d0a343b742df4793268b80767->GetDefaults()));
            [$_23ab4dd81b6c619f47099ef7c2485a12, $total] = $this->get_roles->GetRoles($_03e9164d0a343b742df4793268b80767);
            $response = $this->formatter->Paginate($request, $this->formatter->FormatRoles($_23ab4dd81b6c619f47099ef7c2485a12, $_03e9164d0a343b742df4793268b80767), $_03e9164d0a343b742df4793268b80767->GetChanged(), $total);
        } catch (InvalidArgumentException|UnexpectedValueException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_c894747360c6ec5b" . "d8169530eaf9c473"], $exception);
        }
        return new JsonPrettyResponse($response);
    }
    
    
    
    
    
    
    
    
    
    
    
    public function Post(SecurityContext $context, Request $request)
    {
        try {
            $_d1071a032215f0eb5c4a8ef175f7049d = $this->create_role->CreateRole($context, $this->formatter->GetJson($request));
        } catch (PermissionException $exception) {
            throw new RestExForbidden($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_79bd87060993f" . "f622ef212c1e8b" . "6a79a"], $exception);
        } catch (ValidationException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_c894747360c6e" . "c5bd8169530eaf" . "9c473"], $exception);
        } catch (MappingException $exception) {
            throw new RestExError($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_0ef58d1ad10b6ac76" . "2ed9c5940c6feb7"], 500, $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_3efc5ba7" . "bf51ca709" . "07f5e65ab" . "602ec1"], $exception);
        } catch (InvalidArgumentException|UnexpectedValueException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_c894747360c6ec5bd8169530eaf9c" . "473"], $exception);
        }
        return new JsonPrettyResponse($this->formatter->FormatRole($_d1071a032215f0eb5c4a8ef175f7049d), 201);
    }
    
    
    
    
    
    
    
    
    
    
    
    public function Put(SecurityContext $context, Request $request)
    {
        try {
            $_d1071a032215f0eb5c4a8ef175f7049d = $this->update_role->UpdateRole($context, $this->formatter->GetRequestParam($request, $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["rol" . "e_i" . "d"]), $this->formatter->GetJson($request));
            return new JsonPrettyResponse($this->formatter->FormatRole($_d1071a032215f0eb5c4a8ef175f7049d));
        } catch (NotFoundException $exception) {
            throw new RestExNotFound($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_a4a10182" . "58adabb74" . "e7b04836d" . "8d7e3b"], $exception);
        } catch (PermissionException $exception) {
            throw new RestExForbidden($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_79bd87060993ff622ef212c1e8b6a7" . "9a"], $exception);
        } catch (ValidationException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_c894747360c6ec5bd8" . "169530eaf9c473"], $exception);
        } catch (MappingException $exception) {
            throw new RestExError($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_3682611b4932733eb97863fa0b396" . "700"], 500, $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_3efc5ba7bf51ca70907f5e65ab602ec" . "1"], $exception);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    public function Delete(SecurityContext $context, Request $request)
    {
        try {
            $this->delete_role->DeleteRole($context, $this->formatter->GetRequestParam($request, $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["rol" . "e_i" . "d"]));
        } catch (NotFoundException $exception) {
            throw new RestExNotFound($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_a4a1018" . "258adabb" . "74e7b048" . "36d8d7e3" . "b"], $exception);
        } catch (PermissionException $exception) {
            throw new RestExForbidden($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_79bd87060993ff622e" . "f212c1e8b6a79a"], $exception);
        } catch (MappingException $exception) {
            throw new RestExError($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_67b69c042916369a28c74a7c8b278" . "522"], 500, $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_3efc5ba7bf51ca709" . "07f5e65ab602ec1"], $exception);
        }
        return new JsonPrettyResponse(null, 204);
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    public function GetUsers(SecurityContext $context, Request $request)
    {
        try {
            $_03e9164d0a343b742df4793268b80767 = new UserFilter($context);
            $_03e9164d0a343b742df4793268b80767->Fill($this->formatter->GetRequestParams($request, $_03e9164d0a343b742df4793268b80767->GetDefaults()));
            list($_e4543e131c67c631b14751cc849d1310, $total) = $this->get_role_users->GetRoleUsers($context, $this->formatter->GetRequestParam($request, $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["rol" . "e_i" . "d"]), $_03e9164d0a343b742df4793268b80767);
            $response = $this->formatter->Paginate($request, $this->formatter->FormatUsers($_e4543e131c67c631b14751cc849d1310, $_03e9164d0a343b742df4793268b80767), $_03e9164d0a343b742df4793268b80767->GetChangedWithout([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_25fb31f00b26" . "bde87effd4759" . "e283d9f"]]), $total);
        } catch (NotFoundException $exception) {
            throw new RestExNotFound($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_94c782c8180766e1ff" . "1870975a93053d"], $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_a4a1018258adabb74e7b04836d8d7e" . "3b"], $exception);
        } catch (PermissionException $exception) {
            throw new RestExForbidden($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_79bd87060993ff622ef212c1e8" . "b6a79a"], $exception);
        } catch (InvalidArgumentException|UnexpectedValueException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_c894747360c6ec5bd81" . "69530eaf9c473"], $exception);
        }
        return new JsonPrettyResponse($response);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function PutUsers(SecurityContext $context, Request $request)
    {
        try {
            $_e4543e131c67c631b14751cc849d1310 = $this->set_role_users->SetRoleUsers($context, $this->formatter->GetRequestParam($request, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["ro" . "le" . "_i" . "d"]), $this->formatter->PluckRequestIds($request));
            return new JsonPrettyResponse($this->formatter->FormatUsers($_e4543e131c67c631b14751cc849d1310));
        } catch (NotFoundException $exception) {
            throw new RestExNotFound($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_a4a1018258a" . "dabb74e7b048" . "36d8d7e3b"], $exception);
        } catch (PermissionException $exception) {
            throw new RestExForbidden($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_79bd87060993ff622ef212" . "c1e8b6a79a"], $exception);
        } catch (ValidationException $exception) {
            throw new RestExBadRequest($exception->getMessage(), $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_c8947473" . "60c6ec5bd" . "8169530ea" . "f9c473"], $exception);
        } catch (MappingException $exception) {
            throw new RestExError($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_aabddb8e5540f596ece10005c132c6" . "b6"], 500, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_3efc5ba7bf51ca70907f5e65a" . "b602ec1"], $exception);
        }
    }
}