-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tamás András Horváth <[email protected]>
- Loading branch information
Showing
10 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Handler\Stat; | ||
|
||
use App\Model\MailLogExportModel; | ||
use Laminas\Diactoros\Stream; | ||
use Laminas\Diactoros\Response; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function ob_start; | ||
use function ob_get_clean; | ||
use function fopen; | ||
use function fputcsv; | ||
use function rewind; | ||
use function strval; | ||
|
||
final class GetMailHandler implements RequestHandlerInterface | ||
{ | ||
public function __construct( | ||
private MailLogExportModel $mailLogExportModel, | ||
) { | ||
$this->mailLogExportModel = $mailLogExportModel; | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$exportData = $this->mailLogExportModel->getCsvData(); | ||
|
||
ob_start(); | ||
ob_get_clean(); | ||
|
||
$stream = fopen('php://memory', 'wb+'); | ||
|
||
foreach ($exportData as $fields) { | ||
fputcsv($stream, $fields, ";"); | ||
} | ||
|
||
rewind($stream); | ||
|
||
$body = new Stream($stream); | ||
|
||
return new Response($body, 200, [ | ||
'Content-Type' => 'text/csv; charset=utf-8', | ||
'Content-Disposition' => "attachment; filename=\"mails.csv\"", | ||
'Content-Description' => 'File Transfer', | ||
'Pragma' => 'public', | ||
'Expires' => '0', | ||
'Cache-Control' => 'must-revalidate', | ||
'Content-Length' => strval($body->getSize()), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Handler\Stat; | ||
|
||
use App\Model\MailLogExportModel; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class GetMailHandlerFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): GetMailHandler | ||
{ | ||
return new GetMailHandler( | ||
$container->get(MailLogExportModel::class) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Middleware; | ||
|
||
use App\Service\AccountServiceInterface; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function strlen; | ||
|
||
class StatisticsAccountMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct( | ||
private array $config, | ||
private AccountServiceInterface $accountService | ||
) { | ||
$this->accountService = $accountService; | ||
} | ||
|
||
public function process( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler | ||
): ResponseInterface { | ||
$authCode = $request->getHeaderLine('Authorization'); | ||
|
||
if ( | ||
$authCode !== $this->config['app']['stat']['token'] || | ||
strlen($authCode) === 0 | ||
) { | ||
return new JsonResponse([ | ||
'message' => 'No authentication', | ||
], 401); | ||
} | ||
|
||
return $handler->handle($request); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/App/src/Middleware/StatisticsAccountMiddlewareFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Middleware; | ||
|
||
use App\Service\AccountServiceInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class StatisticsAccountMiddlewareFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): StatisticsAccountMiddleware | ||
{ | ||
$config = $container->has('config') ? $container->get('config') : []; | ||
|
||
return new StatisticsAccountMiddleware( | ||
$config, | ||
$container->get(AccountServiceInterface::class) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Model; | ||
|
||
interface ExportCsvModelInterface | ||
{ | ||
public const DATE_FORMAT = 'Y-m-d H:i:s'; | ||
|
||
public function getCsvData(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Model; | ||
|
||
use App\Entity\MailLog; | ||
use App\Repository\MailLogRepositoryInterface; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
final class MailLogExportModel implements ExportCsvModelInterface | ||
{ | ||
public const HEADER = [ | ||
'id', | ||
'message_id', | ||
'name', | ||
'created_at', | ||
'updated_at', | ||
]; | ||
|
||
/** @var MailLogRepositoryInterface **/ | ||
private $mailLogRepository; | ||
|
||
public function __construct( | ||
private EntityManagerInterface $em | ||
) { | ||
$this->em = $em; | ||
$this->mailLogRepository = $this->em->getRepository(MailLog::class); | ||
} | ||
|
||
public function getCsvData(): array | ||
{ | ||
$mailLogs = $this->mailLogRepository->findAll(); | ||
|
||
$exportData = []; | ||
|
||
$exportData[] = self::HEADER; | ||
|
||
foreach ($mailLogs as $mailLog) { | ||
$data = [ | ||
$mailLog->getId(), | ||
$mailLog->getMessageId(), | ||
$mailLog->getName(), | ||
$mailLog->getCreatedAt()->format(self::DATE_FORMAT), | ||
$mailLog->getUpdatedAt()->format(self::DATE_FORMAT), | ||
]; | ||
|
||
$exportData[] = $data; | ||
} | ||
|
||
return $exportData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Model; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class MailLogExportModelFactory | ||
{ | ||
/** | ||
* @return MailLogExportModel | ||
*/ | ||
public function __invoke(ContainerInterface $container) | ||
{ | ||
return new MailLogExportModel( | ||
$container->get(EntityManagerInterface::class) | ||
); | ||
} | ||
} |