AbstractLogFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use Interop\Container\ContainerInterface;
6
use Psr\Log\LoggerInterface;
7
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
8
9
/**
10
 * @author Pedro Alves <[email protected]>
11
 */
12
abstract class AbstractLogFactory extends AbstractFactory
13
{
14
    
15
    /**
16
     * {@inheritDoc}
17
     */
18 30
    protected function create(ContainerInterface $container, $config)
19
    {
20 30
        $logName = $config[$this->getKey()];
21 30
        return $this->getLoggerOrThrowException($container, $logName);
22
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 8
    public function getServiceType()
28
    {
29 8
        return 'loggers';
30
    }
31
    
32
    /**
33
     * @param ContainerInterface $container
34
     * @param string $logName
35
     * @return LoggerInterface
36
     * @throws ServiceNotCreatedException
37
     */
38 30
    private function getLoggerOrThrowException(ContainerInterface $container, $logName)
39
    {
40 30
        $logger = $this->getServiceOrClassObject($container, $logName);
41
        
42 30
        if (!$logger instanceof LoggerInterface) {
43 2
            throw new ServiceNotCreatedException("There is no '$logName' log");
44
        }
45 28
        return $logger;
46
    }
47
    
48
    /**
49
     * Key in log configuration
50
     * 
51
     * @return string
52
     */
53
    abstract protected function getKey();
54
}
55