Skip to content

Commit 9fbb768

Browse files
committed
[TASK] Add own LogLevel enum and more tests
Relates: #752
1 parent ffab4a6 commit 9fbb768

10 files changed

Lines changed: 370 additions & 77 deletions

File tree

‎Classes/Controller/Backend/Order/PaymentController.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626

2727
public function updateAction(Payment $payment): ResponseInterface
2828
{
29+
// todo: add logging here
2930
$this->paymentRepository->update($payment);
3031

3132
$event = new UpdateServiceEvent($payment);

‎Classes/Controller/Backend/Order/ShippingController.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626

2727
public function updateAction(Shipping $shipping): ResponseInterface
2828
{
29+
// todo: add logging here
2930
$this->shippingRepository->update($shipping);
3031

3132
$event = new UpdateServiceEvent($shipping);

‎Classes/Domain/Log/DatabaseWriter.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public function writeLog(LogRecord $record): WriterInterface
2525
unset($recordData['log']);
2626

2727
$fieldValues = [
28-
'identifier' => $log->getIdentifier(),
29-
'message' => $record->getMessage(),
28+
'log_level' => $log->getLogLevel()->value,
29+
'item' => $log->getOrderItemId(),
30+
'type' => $log->getType(),
31+
'message' => $log->getMessage(),
3032
'arguments' => $this->jsonEncodeWithThrowable($log->getArguments()),
3133
'request_id' => $record->getRequestId(),
3234
'time_micro' => $record->getCreated(),

‎Classes/Domain/Log/LogService.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function write(
1717
LogInterface $log
1818
): void {
1919
$this->logger->log(
20-
$log->getLogLevel(),
20+
$log->getLogLevel()->value,
2121
$log->getMessage(),
2222
[
2323
'log' => $log,

‎Classes/Domain/Log/Model/Log.php‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,89 @@
44

55
namespace Extcode\Cart\Domain\Log\Model;
66

7-
use TYPO3\CMS\Core\Log\LogLevel;
8-
97
final readonly class Log implements LogInterface
108
{
119
public function __construct(
12-
private string $logLevel,
13-
private string $identifier,
10+
private LogLevel $logLevel,
11+
private int $orderItemId,
12+
private string $type,
1413
private string $message,
1514
private array $arguments = [],
1615
) {}
1716

1817
public static function info(
19-
string $identifier,
18+
int $orderItemId,
19+
string $type,
2020
string $message,
2121
array $arguments = [],
2222
): self {
2323
return new self(
2424
LogLevel::INFO,
25-
$identifier,
25+
$orderItemId,
26+
$type,
2627
$message,
2728
$arguments,
2829
);
2930
}
3031

3132
public static function notice(
32-
string $identifier,
33+
int $orderItemId,
34+
string $type,
3335
string $message,
3436
array $arguments = [],
3537
): self {
3638
return new self(
3739
LogLevel::NOTICE,
38-
$identifier,
40+
$orderItemId,
41+
$type,
3942
$message,
4043
$arguments,
4144
);
4245
}
4346

4447
public static function warning(
45-
string $identifier,
48+
int $orderItemId,
49+
string $type,
4650
string $message,
4751
array $arguments = [],
4852
): self {
4953
return new self(
5054
LogLevel::WARNING,
51-
$identifier,
55+
$orderItemId,
56+
$type,
5257
$message,
5358
$arguments,
5459
);
5560
}
5661

5762
public static function error(
58-
string $identifier,
63+
int $orderItemId,
64+
string $type,
5965
string $message,
6066
array $arguments = [],
6167
): self {
6268
return new self(
6369
LogLevel::ERROR,
64-
$identifier,
70+
$orderItemId,
71+
$type,
6572
$message,
6673
$arguments,
6774
);
6875
}
6976

70-
public function getLogLevel(): string
77+
public function getLogLevel(): LogLevel
7178
{
7279
return $this->logLevel;
7380
}
7481

75-
public function getIdentifier(): string
82+
public function getOrderItemId(): int
83+
{
84+
return $this->orderItemId;
85+
}
86+
87+
public function getType(): string
7688
{
77-
return $this->identifier;
89+
return $this->type;
7890
}
7991

8092
public function getMessage(): string

‎Classes/Domain/Log/Model/LogInterface.php‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,46 @@
77
interface LogInterface
88
{
99
public function __construct(
10-
string $logLevel,
11-
string $identifier,
10+
LogLevel $logLevel,
11+
int $orderItemId,
12+
string $type,
1213
string $message,
1314
array $arguments = [],
1415
);
1516

1617
public static function info(
17-
string $identifier,
18+
int $orderItemId,
19+
string $type,
1820
string $message,
1921
array $arguments = [],
2022
): self;
2123

2224
public static function notice(
23-
string $identifier,
25+
int $orderItemId,
26+
string $type,
2427
string $message,
2528
array $arguments = [],
2629
): self;
2730

2831
public static function warning(
29-
string $identifier,
32+
int $orderItemId,
33+
string $type,
3034
string $message,
3135
array $arguments = [],
3236
): self;
3337

3438
public static function error(
35-
string $identifier,
39+
int $orderItemId,
40+
string $type,
3641
string $message,
3742
array $arguments = [],
3843
): self;
3944

40-
public function getLogLevel(): string;
45+
public function getLogLevel(): LogLevel;
4146

42-
public function getIdentifier(): string;
47+
public function getOrderItemId(): int;
48+
49+
public function getType(): string;
4350

4451
public function getMessage(): string;
4552

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\Cart\Domain\Log\Model;
6+
7+
use Psr\Log\LogLevel as PsrLogLevel;
8+
9+
enum LogLevel: string
10+
{
11+
case EMERGENCY = PsrLogLevel::EMERGENCY;
12+
case ALERT = PsrLogLevel::ALERT;
13+
case CRITICAL = PsrLogLevel::CRITICAL;
14+
case ERROR = PsrLogLevel::ERROR;
15+
case WARNING = PsrLogLevel::WARNING;
16+
case NOTICE = PsrLogLevel::NOTICE;
17+
case INFO = PsrLogLevel::INFO;
18+
case DEBUG = PsrLogLevel::DEBUG;
19+
}

‎Classes/Service/MailHandler.php‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Extcode\Cart\Domain\Model\Order\AddressInterface;
1717
use Extcode\Cart\Domain\Model\Order\Item;
1818
use Extcode\Cart\Event\Mail\AttachmentEvent;
19+
use InvalidArgumentException;
1920
use Psr\EventDispatcher\EventDispatcherInterface;
2021
use Psr\Http\Message\ServerRequestInterface;
2122
use Symfony\Component\Mime\Address;
@@ -340,7 +341,8 @@ public function sendBuyerMail(Item $orderItem): void
340341
$this->mailer->send($email);
341342
$this->logService->write(
342343
Log::info(
343-
(string)$orderItem->getUid(),
344+
$this->getOrderItemUid($orderItem),
345+
'sendBuyerMail',
344346
'Mail was send to buyer.',
345347
[
346348
'time' => time(),
@@ -350,7 +352,8 @@ public function sendBuyerMail(Item $orderItem): void
350352
} catch (Exception $e) {
351353
$this->logService->write(
352354
Log::error(
353-
(string)$orderItem->getUid(),
355+
$this->getOrderItemUid($orderItem),
356+
'sendBuyerMail',
354357
'Mail could not send to buyer.',
355358
[
356359
'time' => time(),
@@ -366,6 +369,9 @@ public function sendBuyerMail(Item $orderItem): void
366369
*/
367370
public function sendSellerMail(Item $orderItem): void
368371
{
372+
if (is_null($orderItem->getUid())) {
373+
throw new InvalidArgumentException('Method should only called for persisted orders.', 1774715307);
374+
}
369375
$sellerEmailTo = $this->getSellerEmailTo();
370376
if (empty($this->getSellerEmailFrom()) || empty($sellerEmailTo)) {
371377
return;
@@ -410,7 +416,8 @@ public function sendSellerMail(Item $orderItem): void
410416
$this->mailer->send($email);
411417
$this->logService->write(
412418
Log::info(
413-
(string)$orderItem->getUid(),
419+
$this->getOrderItemUid($orderItem),
420+
'sendSellerMail',
414421
'Mail was send to seller.',
415422
[
416423
'time' => time(),
@@ -420,7 +427,8 @@ public function sendSellerMail(Item $orderItem): void
420427
} catch (Exception $e) {
421428
$this->logService->write(
422429
Log::error(
423-
(string)$orderItem->getUid(),
430+
$this->getOrderItemUid($orderItem),
431+
'sendSellerMail',
424432
'Mail could not send to seller.',
425433
[
426434
'time' => time(),
@@ -445,7 +453,8 @@ public function addAttachments(string $type, Item $orderItem, FluidEmail $email)
445453
} else {
446454
$this->logService->write(
447455
Log::warning(
448-
(string)$orderItem->getUid(),
456+
$this->getOrderItemUid($orderItem),
457+
'addAttachments',
449458
'Mail could add attachment ' . $attachment . ' to mail.',
450459
[
451460
'time' => time(),
@@ -456,4 +465,15 @@ public function addAttachments(string $type, Item $orderItem, FluidEmail $email)
456465
}
457466
}
458467
}
468+
469+
private function getOrderItemUid(Item $orderItem): int
470+
{
471+
$orderItemUid = $orderItem->getUid();
472+
473+
if (is_null($orderItemUid)) {
474+
throw new InvalidArgumentException('Method should only called for persisted orders.', 1774715307);
475+
}
476+
477+
return $orderItemUid;
478+
}
459479
}

0 commit comments

Comments
 (0)