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

MailOptionsAbstractFactory::__invoke()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 10
cts 10
cp 1
rs 5.3846
cc 8
eloc 17
nc 16
nop 3
crap 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