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

MailOptionsAbstractFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 8
c 8
b 2
f 0
lcom 0
cbo 3
dl 0
loc 47
ccs 16
cts 16
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 30 8
1
<?php
2
namespace AcMailer\Options\Factory;
3
4
use AcMailer\Factory\AbstractAcMailerFactory;
5
use AcMailer\Options\MailOptions;
6
use Interop\Container\ContainerInterface;
7
use Interop\Container\Exception\ContainerException;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
use Zend\ServiceManager\Exception\ServiceNotFoundException;
10
use Zend\Stdlib\ArrayUtils;
11
12
/**
13
 * Class MailOptionsAbstractFactory
14
 * @author Alejandro Celaya Alastrué
15
 * @link http://www.alejandrocelaya.com
16
 */
17
class MailOptionsAbstractFactory extends AbstractAcMailerFactory
18
{
19
    const SPECIFIC_PART = 'mailoptions';
20
21
    /**
22
     * Create an object
23
     *
24
     * @param  ContainerInterface $container
25
     * @param  string $requestedName
26 7
     * @param  null|array $options
27
     * @return object
28 7
     * @throws ServiceNotFoundException if unable to resolve the service.
29 7
     * @throws ServiceNotCreatedException if an exception is raised when
30 7
     *     creating a service.
31 7
     * @throws ContainerException if any other error occurs
32 1
     */
33
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
34
    {
35
        $specificServiceName = explode('.', $requestedName)[2];
36
        $config = $this->getConfig($container);
37 7
        $specificConfig = $config[$specificServiceName];
38 3
        if (! is_array($specificConfig)) {
39 7
            $specificConfig = [];
40
        }
41
42
        do {
43 7
            // Get extends
44
            $extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends'])
45
                ? trim($specificConfig['extends'])
46 7
                : null;
47 7
48 7
            // Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an
49
            // exception
50 3
            unset($specificConfig['extends']);
51
52 7
            // Try to extend from another configuration if defined and exists
53
            if (! is_null($extendsConfigKey)
54 7
                && array_key_exists($extendsConfigKey, $config)
55
                && is_array($config[$extendsConfigKey])
56
            ) {
57
                $specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig);
58
            }
59
        } while ($extendsConfigKey != null);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $extendsConfigKey of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
60
61
        return new MailOptions($specificConfig);
62
    }
63
}
64