MollieLoggerAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A addLog() 0 8 2
A canSaveLog() 0 15 4
A addNegativeLog() 0 8 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Logger;
13
14
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
15
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
16
use BitBag\SyliusMolliePlugin\Factory\MollieLoggerFactoryInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Symfony\Component\HttpFoundation\Response;
19
20
final class MollieLoggerAction implements MollieLoggerActionInterface
21
{
22
    /** @var MollieLoggerFactoryInterface */
23
    private $loggerFactory;
24
25
    /** @var RepositoryInterface */
26
    private $repository;
27
28
    /** @var RepositoryInterface */
29
    private $gatewayRepository;
30
31
    public function __construct(
32
        MollieLoggerFactoryInterface $loggerFactory,
33
        RepositoryInterface $repository,
34
        RepositoryInterface $gatewayRepository
35
    ) {
36
        $this->loggerFactory = $loggerFactory;
37
        $this->repository = $repository;
38
        $this->gatewayRepository = $gatewayRepository;
39
    }
40
41
    public function addLog(string $message, int $logLevel = self::NOTICE, int $errorCode = Response::HTTP_OK): void
42
    {
43
        if (false === $this->canSaveLog($logLevel)) {
44
            return;
45
        }
46
47
        $logger = $this->loggerFactory->create($message, $logLevel, $errorCode);
48
        $this->repository->add($logger);
49
    }
50
51
    public function addNegativeLog(string $message, int $logLevel = self::ERROR, int $errorCode = Response::HTTP_INTERNAL_SERVER_ERROR): void
52
    {
53
        if (false === $this->canSaveLog($logLevel)) {
54
            return;
55
        }
56
57
        $logger = $this->loggerFactory->create($message, $logLevel, $errorCode);
58
        $this->repository->add($logger);
59
    }
60
61
    private function canSaveLog(int $logLevel): bool
62
    {
63
        /** @var GatewayConfigInterface $gatewayConfig */
64
        $gatewayConfig = $this->gatewayRepository->findOneBy(['factoryName' => MollieGatewayFactory::FACTORY_NAME]);
65
        $level = $gatewayConfig->getConfig()['loggerLevel'];
66
67
        if ($level === MollieLoggerActionInterface::LOG_EVERYTHING) {
68
            return true;
69
        }
70
71
        if ($level === MollieLoggerActionInterface::LOG_ERRORS && $logLevel === self::ERROR) {
72
            return true;
73
        }
74
75
        return false;
76
    }
77
}
78