Completed
Push — master ( 3b415d...9c7552 )
by Alejandro
02:33
created

getSpecificServiceName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0068

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.4286
cc 3
eloc 10
nc 3
nop 1
crap 3.0068
1
<?php
2
namespace AcMailer\Controller\Plugin\Factory;
3
4
use AcMailer\Controller\Plugin\SendMailPlugin;
5
use AcMailer\Factory\AbstractAcMailerFactory;
6
use AcMailer\Service\Factory\MailServiceAbstractFactory;
7
use AcMailer\Service\MailServiceInterface;
8
use Zend\Filter\Word\CamelCaseToUnderscore;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
11
12
/**
13
 * Class SendMailPluginAbstractFactory
14
 * @author Alejandro Celaya Alastrué
15
 * @link http://www.alejandrocelaya.com
16
 */
17
class SendMailPluginAbstractFactory extends AbstractAcMailerFactory
18
{
19
    /**
20
     * Determine if we can create a service with name
21
     *
22
     * @param ServiceLocatorInterface $serviceLocator
23
     * @param $name
24
     * @param $requestedName
25
     * @return bool
26
     */
27 3
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
28
    {
29
        /** @var ControllerPluginManager $serviceLocator */
30 3
        if (strpos($requestedName, 'sendMail') !== 0) {
31 1
            return false;
32
        }
33
34 2
        if ($requestedName === 'sendMail') {
35 1
            return true;
36
        }
37
38 1
        $specificServiceName = $this->getSpecificServiceName($requestedName);
39 1
        return array_key_exists($specificServiceName, $this->getConfig($serviceLocator->getServiceLocator()));
40
    }
41
42
    /**
43
     * Create service with name
44
     *
45
     * @param ServiceLocatorInterface $serviceLocator
46
     * @param $name
47
     * @param $requestedName
48
     * @return mixed
49
     */
50 1
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
51
    {
52
        /** @var ControllerPluginManager $serviceLocator */
53 1
        $specificServiceName = $this->getSpecificServiceName($requestedName);
54
        /** @var MailServiceInterface $mailService */
55 1
        $mailService = $serviceLocator->getServiceLocator()->get(
56 1
            sprintf('%s.%s.%s', self::ACMAILER_PART, MailServiceAbstractFactory::SPECIFIC_PART, $specificServiceName)
57 1
        );
58 1
        return new SendMailPlugin($mailService);
59
    }
60
61
    /**
62
     * Fetches a mail service name from the requested plugin name.
63
     * sendMailCustomers -> customers
64
     * sendMail -> default
65
     *
66
     * @param $requestedName
67
     * @return string
68
     */
69 2
    protected function getSpecificServiceName($requestedName)
70
    {
71 2
        $filter = new CamelCaseToUnderscore();
72 2
        $parts = explode('_', $filter->filter($requestedName));
73 2
        if (count($parts) === 2) {
74
            return 'default';
75
        }
76
77
        // Discard the sendMail part
78 2
        $parts = array_slice($parts, 2);
79 2
        $specificServiceName = '';
80 2
        foreach ($parts as $part) {
81 2
            $specificServiceName .= $part;
82 2
        }
83
84
        // Convert from camelcase to underscores and set to lower
85 2
        return strtolower($specificServiceName);
86
    }
87
}
88