Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

MonologLoggerFormatterFactory::factory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\MonologLoggerFormatterFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loggers;
22
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use TechDivision\Import\Configuration\Logger\FormatterConfigurationInterface;
25
26
/**
27
 * Monolog Logger formatter factory implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2019 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class MonologLoggerFormatterFactory implements MonologLoggerFormatterFactoryInterface
36
{
37
38
    /**
39
     * The DI container instance.
40
     *
41
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
42
     */
43
    protected $container;
44
45
    /**
46
     * Initialize the factory with the DI container instance.
47
     *
48
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance
49
     */
50
    public function __construct(ContainerInterface $container)
51
    {
52
        $this->container = $container;
53
    }
54
55
    /**
56
     * Returns the DI container instance.
57
     *
58
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance
59
     */
60
    protected function getContainer()
61
    {
62
        return $this->container;
63
    }
64
65
    /**
66
     * Creates a new logger handler formatter instance based on the passed formatter configuration.
67
     *
68
     * @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $formatterConfiguration The formatter configuration
69
     *
70
     * @return \Monolog\Formatter\FormatterInterface The logger handler formatter instance
71
     */
72
    public function factory(FormatterConfigurationInterface $formatterConfiguration)
73
    {
74
75
        // create the formatter (factory) instance
76
        $possibleFormatter = $this->getContainer()->get($formatterConfiguration->getId());
77
        // query whether or not we've a factory or the instance
78
        if ($possibleFormatter instanceof FormatterFactoryInterface) {
79
            return $possibleFormatter->factory($formatterConfiguration);
80
        }
81
82
        // return the formatter instance
83
        return $possibleFormatter;
84
    }
85
}
86