-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogErrorHandlerFactory.php
More file actions
42 lines (33 loc) · 1.48 KB
/
LogErrorHandlerFactory.php
File metadata and controls
42 lines (33 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace Dot\ErrorHandler;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Mezzio\Middleware\ErrorResponseGenerator;
use Laminas\Log\LoggerInterface;
class LogErrorHandlerFactory
{
const ERROR_HANDLER_KEY = 'dot-errorhandler';
const ERROR_HANDLER_LOGGER_KEY = 'logger';
public function __invoke(ContainerInterface $container) : MiddlewareInterface
{
$errorHandlerConfig = $container->get('config')[self::ERROR_HANDLER_KEY] ?? null;
if (!is_array($errorHandlerConfig)) {
throw new InvalidArgumentException(sprintf('\'[%s\'] not found in config', self::ERROR_HANDLER_KEY));
}
if ($errorHandlerConfig['loggerEnabled'] && !isset($errorHandlerConfig[self::ERROR_HANDLER_LOGGER_KEY])) {
throw new InvalidArgumentException(sprintf('Logger: \'[%s\'] is enabled, but not found in config', self::ERROR_HANDLER_LOGGER_KEY));
}
$logger = null;
if ($errorHandlerConfig['loggerEnabled']) {
/** @var LoggerInterface $logger */
$logger = $container->get($errorHandlerConfig['logger']);
}
$generator = $container->has(ErrorResponseGenerator::class)
? $container->get(ErrorResponseGenerator::class)
: null;
return new LogErrorHandler($container->get(ResponseInterface::class), $generator, $logger);
}
}