Completed
Push — develop ( f3c189...e92436 )
by Alejandro
08:55
created

createServiceWithName()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 7
Bugs 2 Features 0
Metric Value
dl 0
loc 30
c 7
b 2
f 0
ccs 19
cts 19
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 Zend\ServiceManager\ServiceLocatorInterface;
7
use Zend\Stdlib\ArrayUtils;
8
9
/**
10
 * Class MailOptionsAbstractFactory
11
 * @author Alejandro Celaya Alastrué
12
 * @link http://www.alejandrocelaya.com
13
 */
14
class MailOptionsAbstractFactory extends AbstractAcMailerFactory
15
{
16
    const SPECIFIC_PART = 'mailoptions';
17
18
    /**
19
     * Create service with name
20
     *
21
     * @param ServiceLocatorInterface $serviceLocator
22
     * @param $name
23
     * @param $requestedName
24
     * @return mixed
25
     */
26 7
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
27
    {
28 7
        $specificServiceName = explode('.', $name)[2];
29 7
        $config = $this->getConfig($serviceLocator);
30 7
        $specificConfig = $config[$specificServiceName];
31 7
        if (! is_array($specificConfig)) {
32 1
            $specificConfig = [];
33 1
        }
34
35
        do {
36
            // Get extends
37 7
            $extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends'])
38 7
                ? trim($specificConfig['extends'])
39 7
                : null;
40
41
            // Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an
42
            // exception
43 7
            unset($specificConfig['extends']);
44
45
            // Try to extend from another configuration if defined and exists
46 7
            if (! is_null($extendsConfigKey)
47 7
                && array_key_exists($extendsConfigKey, $config)
48 7
                && is_array($config[$extendsConfigKey])
49 7
            ) {
50 3
                $specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig);
51 3
            }
52 7
        } 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...
53
54 7
        return new MailOptions($specificConfig);
55
    }
56
}
57