Completed
Pull Request — master (#33)
by Yann
03:02
created

ContentBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 134
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A configure() 0 33 3
A getSubject() 0 15 2
A getBody() 0 25 4
1
<?php
2
3
namespace Yokai\MessengerBundle\Helper;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
use Symfony\Component\Templating\EngineInterface;
7
use Symfony\Component\Translation\TranslatorInterface;
8
use Twig\Environment;
9
use Yokai\MessengerBundle\Exception\BadMethodCallException;
10
11
/**
12
 * @author Yann Eugoné <[email protected]>
13
 */
14
class ContentBuilder
15
{
16
    /**
17
     * @var EngineInterface|null
18
     */
19
    private $templating;
20
21
    /**
22
     * @var Environment|null
23
     */
24
    private $twig;
25
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    private $translator;
30
31
    /**
32
     * @var array
33
     */
34
    private $defaults;
35
36
    /**
37
     * @var array
38
     */
39
    private $options;
40
41
    /**
42
     * @param TranslatorInterface  $translator
43
     * @param array                $defaults
44
     * @param EngineInterface|null $templating
45
     * @param Environment|null     $twig
46
     */
47
    public function __construct(
48
        TranslatorInterface $translator,
49
        array $defaults,
50
        EngineInterface $templating = null,
51
        Environment $twig = null
52
    ) {
53
        $this->twig = $twig;
54
        $this->templating = $templating;
55
        $this->translator = $translator;
56
        $this->defaults = $defaults;
57
    }
58
59
    /**
60
     * @param array $options
61
     */
62
    public function configure($options)
63
    {
64
        $resolver = (new OptionsResolver)
65
            ->setRequired(['template'])
66
            ->setDefault('subject', '')
67
            ->setDefault('translation_catalog', '')
68
            ->setDefault('subject_parameters', [])
69
            ->setDefault('template_parameters', [])
70
            ->setDefault('template_vars', [])
71
            ->setNormalizer('subject_parameters', function ($opts, $value) {
72
                return array_values((array) $value);
73
            })
74
            ->setNormalizer('template_parameters', function ($opts, $value) {
75
                return array_values((array) $value);
76
            })
77
            ->setNormalizer('template_vars', function ($opts, $value) {
78
                return (array) $value;
79
            })
80
        ;
81
82
        foreach ($resolver->getDefinedOptions() as $option) {
83
            if (isset($this->defaults[$option])) {
84
                $resolver->setDefault($option, $this->defaults[$option]);
85
            }
86
        }
87
88
        $options = array_intersect_key(
89
            $options,
90
            array_flip($resolver->getDefinedOptions())
91
        );
92
93
        $this->options = $resolver->resolve($options);
94
    }
95
96
    /**
97
     * @param array $parameters
98
     *
99
     * @return string
100
     */
101
    public function getSubject(array $parameters)
102
    {
103
        if (null === $this->options) {
104
            throw BadMethodCallException::createMissingCall(
105
                __CLASS__.'::configure',
106
                __METHOD__
107
            );
108
        }
109
110
        return $this->translator->trans(
111
            $this->options['subject'],
112
            array_intersect_key($parameters, array_flip($this->options['subject_parameters'])),
113
            $this->options['translation_catalog']
114
        );
115
    }
116
117
    /**
118
     * @param array $parameters
119
     *
120
     * @return string
121
     */
122
    public function getBody(array $parameters)
123
    {
124
        if (null === $this->options) {
125
            throw BadMethodCallException::createMissingCall(
126
                __CLASS__.'::configure',
127
                __METHOD__
128
            );
129
        }
130
131
        $template = strtr(
132
            $this->options['template'],
133
            array_intersect_key($parameters, array_flip($this->options['template_parameters']))
134
        );
135
        $variables = array_merge($parameters, $this->options['template_vars']);
136
137
        if ($this->twig !== null) {
138
            return $this->twig->render($template, $variables);
139
        } elseif ($this->templating !== null) {
140
            return $this->templating->render($template, $variables);
141
        } else {
142
            throw new \LogicException(
143
                sprintf('You can not use %s without "symfony/templating" or "symfony/twig-bundle".', __CLASS__)
144
            );
145
        }
146
    }
147
}
148