ErrorHandlerDelegator::logError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Website\Factory;
5
6
use Interop\Container\ContainerInterface;
7
use Interop\Container\Exception\ContainerException;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Log\LoggerInterface;
10
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
11
use Zend\ServiceManager\Exception\ServiceNotFoundException;
12
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
13
use Zend\Stratigility\Middleware\ErrorHandler;
14
15
class ErrorHandlerDelegator implements DelegatorFactoryInterface
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    private $container;
21
22
    /**
23
     * A factory that creates delegates of a given service
24
     *
25
     * @param  ContainerInterface $container
26
     * @param  string $name
27
     * @param  callable $callback
28
     * @param  null|array $options
29
     * @return object
30
     * @throws ServiceNotFoundException if unable to resolve the service.
31
     * @throws ServiceNotCreatedException if an exception is raised when
32
     *     creating a service.
33
     * @throws ContainerException if any other error occurs
34
     */
35
    public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
36
    {
37
        $this->container = $container;
38
39
        /** @var ErrorHandler $errorHandler */
40
        $errorHandler = $callback();
41
        $errorHandler->attachListener([$this, 'logError']);
42
43
        return $errorHandler;
44
    }
45
46
    public function logError(\Throwable $e, ServerRequestInterface $request): void
47
    {
48
        /** @var LoggerInterface $logger */
49
        $logger = $this->container->get(LoggerInterface::class);
50
        $logger->error(sprintf(
51
            'An error occurred while processing request for URI "%s"',
52
            (string) $request->getUri()
53
        ) . PHP_EOL . $e);
54
    }
55
}
56