Completed
Push — master ( bf1b53...12d63a )
by Nikola
02:46
created

FetchCommandNotificationsCompilerPass::process()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 6
eloc 8
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass;
11
12
use RunOpenCode\Bundle\ExchangeRate\Notification\MailNotification;
13
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Definition;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * Class FetchCommandNotificationsCompilerPass
20
 *
21
 * Fetch command notifications compiler pass
22
 *
23
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass
24
 */
25
class FetchCommandNotificationsCompilerPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        if ($container->hasParameter('run_open_code.exchange_rate.notifications.fetch') && $container->hasDefinition('run_open_code.exchange_rate.command.fetch')) {
33
34
            $configuration = $container->getParameter('run_open_code.exchange_rate.notifications.fetch');
35
36
            if (empty($configuration['to']) && empty($configuration['cc']) && empty($configuration['bcc'])) {
37
                throw new \RuntimeException('Mail notifications for fetch configuration are missing recipients. Did you forget to configure "to", "cc" and/or "bcc" field?');
38
            }
39
40
            $this
41
                ->processSuccessMailNotification($container, $configuration)
42
                ->processErrorMailNotification($container, $configuration)
43
            ;
44
        }
45
    }
46
47
    /**
48
     * Process success mail notifications.
49
     *
50
     * @param ContainerBuilder $container
51
     * @param array $configuration
52
     * @return FetchCommandNotificationsCompilerPass $this Fluent interface.
53
     */
54 View Code Duplication
    public function processSuccessMailNotification(ContainerBuilder $container, array $configuration)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if ($configuration['templates']['success']['enabled']) {
57
58
            $definition = new Definition(MailNotification::class);
59
60
            $definition
61
                ->setArguments(array(
62
                    new Reference('mailer'),
63
                    new Reference('templating'),
64
                    array(
65
                        'from' => $configuration['from'],
66
                        'to' => $configuration['to'],
67
                        'cc' => $configuration['cc'],
68
                        'bcc' => $configuration['bcc'],
69
                        'subject' => $configuration['templates']['success']['subject'],
70
                        'template' => $configuration['templates']['success']['template']
71
                    )
72
                ))
73
                ->setPublic(false)
74
                ;
75
76
            $container->setDefinition('run_open_code.exchange_rate.notifications.mail.fetch_command.success', $definition);
77
78
            $container
79
                ->getDefinition('run_open_code.exchange_rate.command.fetch')
80
                ->addMethodCall('addSuccessNotification', array(
81
                    new Reference('run_open_code.exchange_rate.notifications.mail.fetch_command.success')
82
                ));
83
        }
84
85
        return $this;
86
    }
87
88
    /**
89
     * Process error mail notifications.
90
     *
91
     * @param ContainerBuilder $container
92
     * @param array $configuration
93
     * @return FetchCommandNotificationsCompilerPass $this Fluent interface.
94
     */
95 View Code Duplication
    public function processErrorMailNotification(ContainerBuilder $container, array $configuration)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        if ($configuration['templates']['error']['enabled']) {
98
99
            $definition = new Definition(MailNotification::class);
100
101
            $definition
102
                ->setArguments(array(
103
                    new Reference('mailer'),
104
                    new Reference('templating'),
105
                    array(
106
                        'from' => $configuration['from'],
107
                        'to' => $configuration['to'],
108
                        'cc' => $configuration['cc'],
109
                        'bcc' => $configuration['bcc'],
110
                        'subject' => $configuration['templates']['error']['subject'],
111
                        'template' => $configuration['templates']['error']['template']
112
                    )
113
                ))
114
                ->setPublic(false)
115
            ;
116
117
            $container->setDefinition('run_open_code.exchange_rate.notifications.mail.fetch_command.error', $definition);
118
119
            $container
120
                ->getDefinition('run_open_code.exchange_rate.command.fetch')
121
                ->addMethodCall('addErrorNotification', array(
122
                    new Reference('run_open_code.exchange_rate.notifications.mail.fetch_command.error')
123
                ));
124
        }
125
126
        return $this;
127
    }
128
}
129