Test Failed
Push — master ( ffc597...4abc3b )
by Julien
12:58 queued 09:01
created

ServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 9.52%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 7
eloc 23
c 4
b 3
f 0
dl 0
loc 57
ccs 2
cts 21
cp 0.0952
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 44 7
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Provider\Mailer;
12
13
use Phalcon\Config;
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Mailer\Manager;
16
use InvalidArgumentException;
17
use Zemit\Provider\AbstractServiceProvider;
18
19
/**
20
 * Class ServiceProvider
21
 *
22
 * @author Julien Turbide <[email protected]>
23
 * @copyright Zemit Team <[email protected]>
24
 *
25
 * @since 1.0
26
 * @version 1.0
27
 *
28
 * @package Zemit\Provider\Mailer
29
 */
30
class ServiceProvider extends AbstractServiceProvider
31
{
32
    /**
33
     * The Service name.
34
     * @var string
35
     */
36
    protected $serviceName = 'mailer';
37
    
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @return void
42
     */
43 4
    public function register(DiInterface $di): void
44
    {
45 4
        $di->setShared($this->getName(), function() use ($di) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
46
            
47
            /** @var \Phalcon\Config $config */
48
            $config = $di->get('config');
49
            
50
            /** @var \Phalcon\Config|string $driver */
51
            $driver = $config->path('mailer.driver', 'sendmail');
52
            
53
            /** @var \Phalcon\Events\Manager $eventsManager */
54
            $eventsManager = $di->get('eventsManager');
55
            
56
            switch($driver) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after SWITCH keyword; 0 found
Loading history...
57
                case 'smtp':
58
                case 'mail':
59
                case 'sendmail':
60
                    // Get the mailer manager settings
61
                    $settings = $config->path('mailer.drivers.' . $driver, []);
62
                    $settings = $settings instanceof Config? $settings->toArray() : $settings;
63
                    
64
                    // Get the mailer manager default settings
65
                    $default = $config->path('mailer.default', []);
66
                    $default = $default instanceof Config? $default->toArray() : $default;
67
                    
68
                    // Merge default settings
69
                    $settings = array_merge($settings, $default);
0 ignored issues
show
Bug introduced by
It seems like $settings can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
                    $settings = array_merge(/** @scrutinizer ignore-type */ $settings, $default);
Loading history...
70
                    
71
                    // Prepare the mailer manager
72
                    $manager = new Manager($settings);
73
                    
74
                    // Bind the DI
75
                    $manager->setDI($di);
76
                    
77
                    // Bind the global event manager
78
                    $manager->setEventsManager($eventsManager);
79
                    
80
                    return $manager;
81
            }
82
            
83
            throw new InvalidArgumentException(
84
                sprintf(
85
                    'Invalid mail driver. Expected either "smtp" or "mail" or "sendmail". Got "%s".',
86
                    is_scalar($driver) ? $driver : var_export($driver, true)
0 ignored issues
show
introduced by
The condition is_scalar($driver) is always true.
Loading history...
87
                )
88
            );
89
        });
90
    }
91
}
92