Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

refactor (presenter): Refactor presenter and formatter #12059

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ services:
class: Centreon\Infrastructure\Serializer\ObjectConstructor
public: false

json_presenter:
class: Core\Infrastructure\Common\Presenter\JsonPresenter
json_formatter:
class: Core\Infrastructure\Common\Presenter\JsonFormatter
public: false

Core\Infrastructure\Common\Presenter\PresenterFormatterInterface:
class: Core\Infrastructure\Common\Presenter\JsonPresenter
class: Core\Infrastructure\Common\Presenter\JsonFormatter
public: false

# Encryption
Expand Down Expand Up @@ -95,7 +95,7 @@ services:

presenter.download.csv:
class: Core\Infrastructure\Common\Presenter\DownloadPresenter
arguments: ['@Core\Infrastructure\Common\Presenter\CsvPresenter']
arguments: ['@Core\Infrastructure\Common\Presenter\CsvFormatter']

Core\Application\RealTime\UseCase\FindPerformanceMetrics\FindPerformanceMetricPresenterInterface:
class: Core\Infrastructure\RealTime\Api\DownloadPerformanceMetrics\DownloadPerformanceMetricsPresenter
Expand Down
14 changes: 6 additions & 8 deletions src/Core/Application/Common/UseCase/AbstractPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
namespace Core\Application\Common\UseCase;

use Symfony\Component\HttpFoundation\Response;
use Core\Application\Common\UseCase\PresenterInterface;
use Core\Application\Common\UseCase\ResponseStatusInterface;
use Core\Infrastructure\Common\Presenter\PresenterFormatterInterface;

abstract class AbstractPresenter implements PresenterInterface
{
/**
* @var ResponseStatusInterface|null
*/
protected $responseStatus;
protected ?ResponseStatusInterface $responseStatus = null;

/**
* @var mixed
Expand All @@ -49,10 +47,9 @@ public function __construct(protected PresenterFormatterInterface $presenterForm
/**
* @inheritDoc
*/
public function present(mixed $presentedData): void
public function present(mixed $data): void
{
$this->presentedData = $presentedData;
$this->presenterFormatter->present($presentedData);
$this->presentedData = $data;
}

/**
Expand All @@ -68,7 +65,9 @@ public function getPresentedData(): mixed
*/
public function show(): Response
{
return $this->presenterFormatter->show();
return ($this->responseStatus !== null)
? $this->presenterFormatter->format($this->responseStatus)
: $this->presenterFormatter->format($this->presentedData);
}

/**
Expand All @@ -77,7 +76,6 @@ public function show(): Response
public function setResponseStatus(?ResponseStatusInterface $responseStatus): void
{
$this->responseStatus = $responseStatus;
$this->presenterFormatter->present($responseStatus);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Core/Application/Common/UseCase/PresenterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace Core\Application\Common\UseCase;

use Core\Application\Common\UseCase\ResponseStatusInterface;
use Symfony\Component\HttpFoundation\Response;

interface PresenterInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface FindServicePresenterInterface extends PresenterInterface
{
/**
* {@inheritDoc}
* @param FindServiceResponse $response
* @param FindServiceResponse $data
*/
public function present(mixed $response): void;
public function present(mixed $data): void;
}
48 changes: 48 additions & 0 deletions src/Core/Infrastructure/Common/Presenter/AbstractFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : [email protected]
*
*/

declare(strict_types=1);

namespace Core\Infrastructure\Common\Presenter;

abstract class AbstractFormatter
{
/**
* @var array<string, string>
*/
protected array $responseHeaders = [];

/**
* @param array<string, string> $responseHeaders
*/
public function setResponseHeaders(array $responseHeaders): void
{
$this->responseHeaders = $responseHeaders;
}

/**
* @return array<string, string>
*/
public function getResponseHeaders(): array
{
return $this->responseHeaders;
}
}
102 changes: 0 additions & 102 deletions src/Core/Infrastructure/Common/Presenter/AbstractPresenter.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,27 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

class CsvPresenter extends AbstractPresenter implements PresenterFormatterInterface
class CsvFormatter extends AbstractFormatter implements PresenterFormatterInterface
{
private mixed $data = null;

/**
* @inheritDoc
*/
public function present(mixed $data): void
{
$this->data = $data;
}

/**
* @inheritDoc
*/
public function show(): Response
public function format(mixed $data): Response
{
$response = new StreamedResponse(null, Response::HTTP_OK, $this->responseHeaders);
$response->setCallback(function () {
$response->setCallback(function () use ($data) {
$handle = fopen('php://output', 'r+');
if ($handle === false) {
throw new \RuntimeException('Unable to open the output buffer');
}
$lineHeadersCreated = false;
foreach ($this->data as $data) {
foreach ($data as $oneData) {
if (! $lineHeadersCreated) {
$columnNames = array_keys($data);
$columnNames = array_keys($oneData);
fputcsv($handle, $columnNames, ';');
$lineHeadersCreated = true;
}
$columnValues = array_values($data);
$columnValues = array_values($oneData);
fputcsv($handle, $columnValues, ';');
}

Expand Down
21 changes: 7 additions & 14 deletions src/Core/Infrastructure/Common/Presenter/DownloadPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use Symfony\Component\HttpFoundation\Response;

class DownloadPresenter extends AbstractPresenter implements PresenterFormatterInterface, DownloadInterface
class DownloadPresenter extends AbstractFormatter implements PresenterFormatterInterface, DownloadInterface
{
private const CSV_FILE_EXTENSION = 'csv';
private const JSON_FILE_EXTENSION = 'json';
Expand All @@ -39,25 +39,18 @@ public function __construct(private PresenterFormatterInterface $presenter)
/**
* @inheritDoc
*/
public function present(mixed $data): void
public function format(mixed $data): Response
{
$this->presenter->present($data);
$originalHeaders = $this->presenter->getResponseHeaders();
$originalHeaders['Content-Type'] = 'application/force-download';
$originalHeaders['Content-Disposition'] = 'attachment; filename="' . $this->generateDownloadFileName() . '"';
$this->presenter->setResponseHeaders($originalHeaders);
return $this->presenter->format($data);
}

/**
* @inheritDoc
*/
public function show(): Response
{
return $this->presenter->show();
}

/**
* @inheritDoc
* @param string $fileName
* @return void
*/
public function setDownloadFileName(string $fileName): void
{
Expand All @@ -72,8 +65,8 @@ public function setDownloadFileName(string $fileName): void
private function generateDownloadFileExtension(): string
{
return match (get_class($this->presenter)) {
CsvPresenter::class => self::CSV_FILE_EXTENSION,
JsonPresenter::class => self::JSON_FILE_EXTENSION,
CsvFormatter::class => self::CSV_FILE_EXTENSION,
JsonFormatter::class => self::JSON_FILE_EXTENSION,
default => '',
};
}
Expand Down
Loading