Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Dec 18, 2024
1 parent 1c7ea85 commit 5a6fe09
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 326 deletions.
384 changes: 192 additions & 192 deletions lib/Controller/EndpointsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,180 +21,180 @@
*/
class EndpointsController extends Controller
{
/**
* Constructor for the EndpointsController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param EndpointMapper $endpointMapper The endpoint mapper object
* @param EndpointService $endpointService Service for handling endpoint operations
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private EndpointMapper $endpointMapper,
private EndpointService $endpointService,
/**
* Constructor for the EndpointsController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param EndpointMapper $endpointMapper The endpoint mapper object
* @param EndpointService $endpointService Service for handling endpoint operations
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private EndpointMapper $endpointMapper,
private EndpointService $endpointService,
private AuthorizationService $authorizationService
)
{
parent::__construct($appName, $request);
}

/**
* Returns the template of the main app's page
*
* This method renders the main page of the application, adding any necessary data to the template.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse The rendered template response
*/
public function page(): TemplateResponse
{
return new TemplateResponse(
'openconnector',
'index',
[]
);
}

/**
* Retrieves a list of all endpoints
*
* This method returns a JSON response containing an array of all endpoints in the system.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the list of endpoints
*/
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{

$filters = $this->request->getParams();
$fieldsToSearch = ['name', 'description', 'endpoint'];

$searchParams = $searchService->createMySQLSearchParams(filters: $filters);
$searchConditions = $searchService->createMySQLSearchConditions(filters: $filters, fieldsToSearch: $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams(filters: $filters);

return new JSONResponse(['results' => $this->endpointMapper->findAll(limit: null, offset: null, filters: $filters, searchConditions: $searchConditions, searchParams: $searchParams)]);
}

/**
* Retrieves a single endpoint by its ID
*
* This method returns a JSON response containing the details of a specific endpoint.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the endpoint to retrieve
* @return JSONResponse A JSON response containing the endpoint details
*/
public function show(string $id): JSONResponse
{
try {
return new JSONResponse($this->endpointMapper->find(id: (int) $id));
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}
}

/**
* Creates a new endpoint
*
* This method creates a new endpoint based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the created endpoint
*/
public function create(): JSONResponse
{
$data = $this->request->getParams();

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
unset($data[$key]);
}
}

if (isset($data['id'])) {
unset($data['id']);
}

$endpoint = $this->endpointMapper->createFromArray(object: $data);

return new JSONResponse($endpoint);
}

/**
* Updates an existing endpoint
*
* This method updates an existing endpoint based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the endpoint to update
* @return JSONResponse A JSON response containing the updated endpoint details
*/
public function update(int $id): JSONResponse
{
$data = $this->request->getParams();

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
unset($data[$key]);
}
}
if (isset($data['id'])) {
unset($data['id']);
}

$endpoint = $this->endpointMapper->updateFromArray(id: (int) $id, object: $data);

return new JSONResponse($endpoint);
}

/**
* Deletes an endpoint
*
* This method deletes an endpoint based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the endpoint to delete
* @return JSONResponse An empty JSON response
*/
public function destroy(int $id): JSONResponse
{
$this->endpointMapper->delete($this->endpointMapper->find((int) $id));

return new JSONResponse([]);
}

/**
* Handles generic path requests by matching against registered endpoints
*
* This method checks if the current path matches any registered endpoint patterns
* and forwards the request to the appropriate endpoint service if found
*
* @NoAdminRequired
* @NoCSRFRequired
)
{
parent::__construct($appName, $request);
}

/**
* Returns the template of the main app's page
*
* This method renders the main page of the application, adding any necessary data to the template.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse The rendered template response
*/
public function page(): TemplateResponse
{
return new TemplateResponse(
'openconnector',
'index',
[]
);
}

/**
* Retrieves a list of all endpoints
*
* This method returns a JSON response containing an array of all endpoints in the system.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the list of endpoints
*/
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{

$filters = $this->request->getParams();
$fieldsToSearch = ['name', 'description', 'endpoint'];

$searchParams = $searchService->createMySQLSearchParams(filters: $filters);
$searchConditions = $searchService->createMySQLSearchConditions(filters: $filters, fieldsToSearch: $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams(filters: $filters);

return new JSONResponse(['results' => $this->endpointMapper->findAll(limit: null, offset: null, filters: $filters, searchConditions: $searchConditions, searchParams: $searchParams)]);
}

/**
* Retrieves a single endpoint by its ID
*
* This method returns a JSON response containing the details of a specific endpoint.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the endpoint to retrieve
* @return JSONResponse A JSON response containing the endpoint details
*/
public function show(string $id): JSONResponse
{
try {
return new JSONResponse($this->endpointMapper->find(id: (int)$id));
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}
}

/**
* Creates a new endpoint
*
* This method creates a new endpoint based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the created endpoint
*/
public function create(): JSONResponse
{
$data = $this->request->getParams();

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
unset($data[$key]);
}
}

if (isset($data['id'])) {
unset($data['id']);
}

$endpoint = $this->endpointMapper->createFromArray(object: $data);

return new JSONResponse($endpoint);
}

/**
* Updates an existing endpoint
*
* This method updates an existing endpoint based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the endpoint to update
* @return JSONResponse A JSON response containing the updated endpoint details
*/
public function update(int $id): JSONResponse
{
$data = $this->request->getParams();

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
unset($data[$key]);
}
}
if (isset($data['id'])) {
unset($data['id']);
}

$endpoint = $this->endpointMapper->updateFromArray(id: (int)$id, object: $data);

return new JSONResponse($endpoint);
}

/**
* Deletes an endpoint
*
* This method deletes an endpoint based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the endpoint to delete
* @return JSONResponse An empty JSON response
*/
public function destroy(int $id): JSONResponse
{
$this->endpointMapper->delete($this->endpointMapper->find((int)$id));

return new JSONResponse([]);
}

/**
* Handles generic path requests by matching against registered endpoints
*
* This method checks if the current path matches any registered endpoint patterns
* and forwards the request to the appropriate endpoint service if found
*
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* @param string $path The request path to match
* @return JSONResponse The response from the endpoint service or 404 if no match
*/
public function handlePath(string $_path): JSONResponse
{
*
* @param string $path The request path to match
* @return JSONResponse The response from the endpoint service or 404 if no match
*/
public function handlePath(string $_path): JSONResponse
{
try {
$token = $this->request->getHeader('Authorization');
$this->authorizationService->authorize(authorization: $token);
Expand All @@ -205,25 +205,25 @@ public function handlePath(string $_path): JSONResponse
);
}

// Find matching endpoints for the given path and method
$matchingEndpoints = $this->endpointMapper->findByPathRegex(
path: $_path,
method: $this->request->getMethod()
);

// If no matching endpoints found, return 404
if (empty($matchingEndpoints)) {
return new JSONResponse(
data: ['error' => 'No matching endpoint found for path and method: ' . $_path . ' ' . $this->request->getMethod()],
statusCode: 404
);
}

// Get the first matching endpoint since we already filtered by method
$endpoint = reset($matchingEndpoints);

// Forward the request to the endpoint service
return $this->endpointService->handleRequest($endpoint, $this->request, $_path);
}
// Find matching endpoints for the given path and method
$matchingEndpoints = $this->endpointMapper->findByPathRegex(
path: $_path,
method: $this->request->getMethod()
);

// If no matching endpoints found, return 404
if (empty($matchingEndpoints)) {
return new JSONResponse(
data: ['error' => 'No matching endpoint found for path and method: ' . $_path . ' ' . $this->request->getMethod()],
statusCode: 404
);
}

// Get the first matching endpoint since we already filtered by method
$endpoint = reset($matchingEndpoints);

// Forward the request to the endpoint service
return $this->endpointService->handleRequest($endpoint, $this->request, $_path);
}

}
Loading

0 comments on commit 5a6fe09

Please sign in to comment.