Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

SwiftMailerHandlerFactory::factory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 23
cp 0
rs 9.312
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\SwiftMailerHandlerFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loggers;
22
23
use Monolog\Handler\SwiftMailerHandler;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use TechDivision\Import\Utils\LoggerKeys;
26
use TechDivision\Import\Utils\SwiftMailerKeys;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface;
29
use TechDivision\Import\Loggers\SwiftMailer\TransportMailerFactoryInterface;
30
31
/**
32
 * Swift Mailer Handler factory implementation.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2019 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import
38
 * @link      http://www.techdivision.com
39
 */
40
class SwiftMailerHandlerFactory implements HandlerFactoryInterface
41
{
42
43
    /**
44
     * The system name to use.
45
     *
46
     * @var string
47
     */
48
    protected $systemName;
49
50
    /**
51
     * The DI container instance.
52
     *
53
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
54
     */
55
    protected $container;
56
57
    /**
58
     * Initialize the processor with the actual configuration instance
59
     *
60
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container     The DI container instance
61
     * @param \TechDivision\Import\ConfigurationInterface               $configuration The actual configuration instance
62
     */
63
    public function __construct(ContainerInterface $container, ConfigurationInterface $configuration)
64
    {
65
        $this->container = $container;
66
        $this->systemName = $configuration->getSystemName();
67
    }
68
69
    /**
70
     * Returns the DI container instance.
71
     *
72
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance
73
     */
74
    protected function getContainer()
75
    {
76
        return $this->container;
77
    }
78
79
    /**
80
     * Return's the system name to use.
81
     *
82
     * @return string The system name
83
     */
84
    protected function getSystemName()
85
    {
86
        return $this->systemName;
87
    }
88
89
    /**
90
     * Creates a new formatter instance based on the passed configuration.
91
     *
92
     * @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration The handler configuration
93
     *
94
     * @return \Monolog\Handler\HandlerInterface The handler instance
95
     */
96
    public function factory(HandlerConfigurationInterface $handlerConfiguration)
97
    {
98
99
        // load the swift mailer configuration
100
        $swiftMailerConfiguration = $handlerConfiguration->getSwiftMailer();
101
102
        // create the swift mailer (factory) instance
103
        $possibleSwiftMailer = $this->getContainer()->get($swiftMailerConfiguration->getId());
104
105
        // query whether or not we've a factory or the instance
106
        /** @var \Swift_Mailer $swiftMailer */
107
        if ($possibleSwiftMailer instanceof TransportMailerFactoryInterface) {
108
            $swiftMailer = $possibleSwiftMailer->factory($swiftMailerConfiguration->getTransport());
109
        } else {
110
            $swiftMailer = $possibleSwiftMailer;
111
        }
112
113
        // load the generic logger configuration
114
        $bubble = $handlerConfiguration->getParam(LoggerKeys::BUBBLE);
115
        $logLevel = $handlerConfiguration->getParam(LoggerKeys::LOG_LEVEL);
116
117
        // load sender/receiver configuration
118
        $to = $swiftMailerConfiguration->getParam(SwiftMailerKeys::TO);
119
        $from = $swiftMailerConfiguration->getParam(SwiftMailerKeys::FROM);
120
        $subject = $swiftMailerConfiguration->getParam(SwiftMailerKeys::SUBJECT);
121
        $contentType = $swiftMailerConfiguration->getParam(SwiftMailerKeys::CONTENT_TYPE);
122
123
        // initialize the message template
124
        $message = $swiftMailer->createMessage()
125
            ->setSubject(sprintf('[%s] %s', $this->getSystemName(), $subject))
126
            ->setFrom($from)
127
            ->setTo($to)
128
            ->setBody('', $contentType);
129
130
        // initialize the handler node
131
        $reflectionClass = new \ReflectionClass(SwiftMailerHandler::class);
132
        return $reflectionClass->newInstanceArgs(array($swiftMailer, $message, $logLevel, $bubble));
133
    }
134
}
135