Completed
Push — master ( 00c188...e25488 )
by Yann
03:03
created

ContentBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.73%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 134
ccs 51
cts 55
cp 0.9273
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 10
    public function __construct(
48
        TranslatorInterface $translator,
49
        array $defaults,
50
        EngineInterface $templating = null,
51
        Environment $twig = null
52
    ) {
53 10
        $this->twig = $twig;
54 10
        $this->templating = $templating;
55 10
        $this->translator = $translator;
56 10
        $this->defaults = $defaults;
57 10
    }
58
59
    /**
60
     * @param array $options
61
     */
62 8
    public function configure($options)
63
    {
64 8
        $resolver = (new OptionsResolver)
65 8
            ->setRequired(['template'])
66 8
            ->setDefault('subject', '')
67 8
            ->setDefault('translation_catalog', '')
68 8
            ->setDefault('subject_parameters', [])
69 8
            ->setDefault('template_parameters', [])
70 8
            ->setDefault('template_vars', [])
71 8
            ->setNormalizer('subject_parameters', function ($opts, $value) {
72 8
                return array_values((array) $value);
73 8
            })
74 8
            ->setNormalizer('template_parameters', function ($opts, $value) {
75 8
                return array_values((array) $value);
76 8
            })
77 8
            ->setNormalizer('template_vars', function ($opts, $value) {
78 8
                return (array) $value;
79 8
            })
80
        ;
81
82 8
        foreach ($resolver->getDefinedOptions() as $option) {
83 8
            if (isset($this->defaults[$option])) {
84 8
                $resolver->setDefault($option, $this->defaults[$option]);
85
            }
86
        }
87
88 8
        $options = array_intersect_key(
89 8
            $options,
90 8
            array_flip($resolver->getDefinedOptions())
91
        );
92
93 8
        $this->options = $resolver->resolve($options);
94 8
    }
95
96
    /**
97
     * @param array $parameters
98
     *
99
     * @return string
100
     */
101 4
    public function getSubject(array $parameters)
102
    {
103 4
        if (null === $this->options) {
104 1
            throw BadMethodCallException::createMissingCall(
105 1
                __CLASS__.'::configure',
106 1
                __METHOD__
107
            );
108
        }
109
110 3
        return $this->translator->trans(
111 3
            $this->options['subject'],
112 3
            array_intersect_key($parameters, array_flip($this->options['subject_parameters'])),
113 3
            $this->options['translation_catalog']
114
        );
115
    }
116
117
    /**
118
     * @param array $parameters
119
     *
120
     * @return string
121
     */
122 4
    public function getBody(array $parameters)
123
    {
124 4
        if (null === $this->options) {
125 1
            throw BadMethodCallException::createMissingCall(
126 1
                __CLASS__.'::configure',
127 1
                __METHOD__
128
            );
129
        }
130
131 3
        $template = strtr(
132 3
            $this->options['template'],
133 3
            array_intersect_key($parameters, array_flip($this->options['template_parameters']))
134
        );
135 3
        $variables = array_merge($parameters, $this->options['template_vars']);
136
137 3
        if ($this->twig !== null) {
138 3
            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