<?php
namespace ApiV3Bundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class AiCoachMasterDataController extends BaseController
{
private $authenticator;
private $biz;
public function __construct($authenticator, $biz)
{
$this->authenticator = $authenticator;
$this->biz = $biz;
}
public function departmentAction(Request $request, $sourceId)
{
$claims = $this->authenticator->authenticate($request, 'master-data:projection');
return $this->response($this->service()->upsertDepartment(
(int) $claims['providerBindingId'],
$sourceId,
$this->body($request)
));
}
public function positionGroupAction(Request $request, $sourceId)
{
$claims = $this->authenticator->authenticate($request, 'master-data:projection');
return $this->response($this->service()->upsertPositionGroup(
(int) $claims['providerBindingId'],
$sourceId,
$this->body($request)
));
}
public function positionAction(Request $request, $sourceId)
{
$claims = $this->authenticator->authenticate($request, 'master-data:projection');
return $this->response($this->service()->upsertPosition(
(int) $claims['providerBindingId'],
$sourceId,
$this->body($request)
));
}
public function userProjectionAction(Request $request, $externalId)
{
$claims = $this->authenticator->authenticate($request, 'user:projection');
return $this->response($this->service()->projectUser(
(int) $claims['providerBindingId'],
$externalId,
$this->body($request)
));
}
private function body(Request $request)
{
$fields = json_decode((string) $request->getContent(), true);
if (!is_array($fields)) {
throw new BadRequestHttpException('Request body must be valid JSON');
}
return $fields;
}
private function response(array $mapping)
{
return $this->createJsonResponse([
'id' => (int) $mapping['targetId'],
'version' => (int) $mapping['version'],
'deleted' => !empty($mapping['deleted']),
]);
}
private function service()
{
return $this->biz->service('CorporateTrainingBundle:AiCoachMasterData:AiCoachMasterDataService');
}
}