Skip to content

Commit

Permalink
Add email log stat
Browse files Browse the repository at this point in the history
Signed-off-by: Tamás András Horváth <[email protected]>
  • Loading branch information
icetee committed Jun 14, 2023
1 parent f40674a commit b3be69c
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/autoload/dependencies.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

App\Middleware\AuditMiddleware::class => App\Middleware\AuditMiddlewareFactory::class,
App\Middleware\UserMiddleware::class => App\Middleware\UserMiddlewareFactory::class,
App\Middleware\StatisticsAccountMiddleware::class => App\Middleware\StatisticsAccountMiddlewareFactory::class,

\Middlewares\Recaptcha::class => App\Middleware\RecaptchaMiddlewareFactory::class,

Expand Down
5 changes: 5 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
App\Middleware\UserMiddleware::class,
App\Handler\Account\RejectHandler::class
], 'admin.api.account.reject');

$app->get('/admin/api/stat/mails', [
App\Middleware\StatisticsAccountMiddleware::class,
App\Handler\Stat\GetMailHandler::class
], 'app.api.stat.mails');
};
3 changes: 3 additions & 0 deletions src/App/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public function getDependencies(): array
Handler\Account\PrintHandler::class => Handler\Account\PrintHandlerFactory::class,
Handler\Account\RejectHandler::class => Handler\Account\RejectHandlerFactory::class,
Handler\Setting\GetHandler::class => Handler\Setting\GetHandlerFactory::class,
Handler\Stat\GetMailHandler::class => Handler\Stat\GetMailHandlerFactory::class,
Service\AccountServiceInterface::class => Service\AccountServiceFactory::class,
Service\AuditLogServiceInterface::class => Service\AuditLogServiceFactory::class,
Service\MailQueueServiceInterface::class => Service\MailQueueServiceFactory::class,
Service\UserServiceInterface::class => Service\UserServiceFactory::class,
Service\MailServiceInterface::class => Service\MailServiceFactory::class,
Helper\MailContentHelper::class => Helper\MailContentHelperFactory::class,
Helper\MailContentRawHelper::class => Helper\MailContentRawHelperFactory::class,
Model\MailLogExportModel::class => Model\MailLogExportModelFactory::class,
Middleware\StatisticsAccountMiddleware::class => Middleware\StatisticsAccountMiddlewareFactory::class,
],
];
}
Expand Down
56 changes: 56 additions & 0 deletions src/App/src/Handler/Stat/GetMailHandler.php
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()),
]);
}
}
18 changes: 18 additions & 0 deletions src/App/src/Handler/Stat/GetMailHandlerFactory.php
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)
);
}
}
42 changes: 42 additions & 0 deletions src/App/src/Middleware/StatisticsAccountMiddleware.php
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 src/App/src/Middleware/StatisticsAccountMiddlewareFactory.php
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)
);
}
}
12 changes: 12 additions & 0 deletions src/App/src/Model/ExportCsvModelInterface.php
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;
}
53 changes: 53 additions & 0 deletions src/App/src/Model/MailLogExportModel.php
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;
}
}
21 changes: 21 additions & 0 deletions src/App/src/Model/MailLogExportModelFactory.php
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)
);
}
}

0 comments on commit b3be69c

Please sign in to comment.