Completed
Push — master ( 01eb7b...2ee407 )
by Alejandro
09:40 queued 07:02
created

AbstractAcMailerFactory::canCreate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 9
nc 3
nop 2
crap 4
1
<?php
2
namespace AcMailer\Factory;
3
4
use Interop\Container\ContainerInterface;
5
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
6
use Zend\ServiceManager\Exception\ServiceNotFoundException;
7
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
/**
11
 * Class AbstractAcMailerFactory
12
 * @author Alejandro Celaya Alastrué
13
 * @link http://www.alejandrocelaya.com
14
 */
15
abstract class AbstractAcMailerFactory implements AbstractFactoryInterface
16
{
17
    const ACMAILER_PART = 'acmailer';
18
    const SPECIFIC_PART = '';
19
20
    /**
21
     * Can the factory create an instance for the service?
22
     *
23
     * @param  ContainerInterface $container
24
     * @param  string $requestedName
25 1
     * @return bool
26
     */
27 1
    public function canCreate(ContainerInterface $container, $requestedName)
28 1
    {
29 1
        $parts = explode('.', $requestedName);
30
        if (count($parts) !== 3) {
31
            return false;
32 1
        }
33 1
34
        if ($parts[0] !== self::ACMAILER_PART || $parts[1] !== static::SPECIFIC_PART) {
35
            return false;
36 1
        }
37 1
38 1
        $specificServiceName = $parts[2];
39
        $config = $this->getConfig($container);
40
        return array_key_exists($specificServiceName, $config);
41
    }
42
43
    /**
44
     * @param ContainerInterface $container
45 9
     * @return array
46
     */
47 9
    protected function getConfig(ContainerInterface $container)
48 9
    {
49 8
        $config = $container->get('Config');
50 2
        if (isset($config['acmailer_options']) && is_array($config['acmailer_options'])) {
51 1
            return $config['acmailer_options'];
52
        }
53
54 1
        return [];
55
    }
56
}
57