MailPackage::addToContainer()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 1
nop 1
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 3
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\Mail;
6
7
use Barnacle\Container;
8
use Barnacle\RegistrationInterface;
9
use Bone\View\ViewEngine;
10
use Bone\Server\Environment;
11
use Bone\Server\SiteConfig;
12
use Bone\Mail\Service\MailService;
13
use Laminas\Mail\Transport\Sendmail;
14
use Laminas\Mail\Transport\Smtp;
15
use Laminas\Mail\Transport\SmtpOptions;
16
17
class MailPackage implements RegistrationInterface
18
{
19
    /**
20
     * @param Container $c
21
     */
22 1
    public function addToContainer(Container $c)
23
    {
24 1
        $c[MailService::class] = $c->factory(function (Container $c) {
25 1
            $view = $c->get(ViewEngine::class);
26 1
            $siteConfig = $c->get(SiteConfig::class);
27 1
            $mailService = new MailService();
28 1
            $transport = new Sendmail();
29
30 1
            if ($c->has('mail')) {
31 1
                $settings = $c->get('mail');
32
33 1
                if (isset($settings['name'], $settings['host'], $settings['port'])) {
34 1
                    $options = new SmtpOptions($settings);
35 1
                    $transport = new Smtp($options);
36
                }
37
            }
38
39 1
            $mailService->setView($view);
40 1
            $mailService->setSiteConfig($siteConfig);
41 1
            $mailService->setTransport($transport);
42
43 1
            return $mailService;
44 1
        });
45
    }
46
}
47