Completed
Push — develop ( b8f7b1...cea6ad )
by
unknown
07:05
created

ErrorLoggerFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 18.03 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 11
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __invoke() 11 36 7
A createService() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** ErrorLoggerFactory.php */
11
namespace Core\Log;
12
13
use Interop\Container\ContainerInterface;
14
use Interop\Container\Exception\ContainerException;
15
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
16
use Zend\ServiceManager\Exception\ServiceNotFoundException;
17
use Zend\ServiceManager\FactoryInterface;
18
use Zend\ServiceManager\ServiceLocatorInterface;
19
use Zend\Log\Logger;
20
use Zend\Log\Writer\Stream;
21
use Core\Log\Filter\ErrorType;
22
use Core\Log\Formatter\ErrorAndExceptionHandler;
23
24
class ErrorLoggerFactory implements FactoryInterface
25
{
26
    /**
27
     * Create an object
28
     *
29
     * @param  ContainerInterface $container
30
     * @param  string             $requestedName
31
     * @param  null|array         $options
32
     *
33
     * @return object
34
     * @throws ServiceNotFoundException if unable to resolve the service.
35
     * @throws ServiceNotCreatedException if an exception is raised when
36
     *     creating a service.
37
     * @throws ContainerException if any other error occurs
38
     */
39
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
40
    {
41
        $config = $container->get('Config');
42
        $config = isset($config['log']['ErrorLogger']['config'])
43
            ? $config['log']['ErrorLogger']['config']
44
            : array();
45
46
        if (!isset($config['stream'])) {
47
            throw new \RuntimeException('A stream must be configured for ErrorLogger.');
48
        }
49
50
        $formatter = new ErrorAndExceptionHandler(
51
            array(
52
                'dateTimeFormat' => 'Y-m-d H:i:s',
53
            )
54
        );
55
        $writer = new Stream($config['stream']);
56
        $writer->setFormatter($formatter);
57
        $logger = new Logger();
58
59 View Code Duplication
        if (!isset($config['log_errors']) || !$config['log_errors']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $writer->addFilter(new ErrorType(ErrorType::TYPE_EXCEPTION));
61
62
        } else {
63
            Logger::registerErrorHandler($logger);
64
        }
65
66 View Code Duplication
        if (!isset($config['log_exceptions']) || !$config['log_exceptions']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $writer->addFilter(new ErrorType(ErrorType::TYPE_ERROR));
68
        } else {
69
            Logger::registerExceptionHandler($logger);
70
        }
71
        $logger->addWriter($writer);
72
        return $logger;
73
74
    }
75
76
77
    /* (non-PHPdoc)
78
     * @see \Zend\ServiceManager\FactoryInterface::createService()
79
     */
80
    public function createService(ServiceLocatorInterface $serviceLocator)
81
    {
82
        return $this($serviceLocator, Logger::class);
83
    }
84
}
85