Completed
Pull Request — master (#33)
by Yann
11:23
created

ContentBuilder::getBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Yokai\MessengerBundle\Helper;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
use Symfony\Component\Translation\TranslatorInterface;
7
use Twig\Environment;
8
use Yokai\MessengerBundle\Exception\BadMethodCallException;
9
10
/**
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class ContentBuilder
14
{
15
    /**
16
     * @var Environment
17
     */
18
    private $twig;
19
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    private $translator;
24
25
    /**
26
     * @var array
27
     */
28
    private $defaults;
29
30
    /**
31
     * @var array
32
     */
33
    private $options;
34
35
    /**
36
     * @param Environment         $twig
37
     * @param TranslatorInterface $translator
38
     * @param array               $defaults
39
     */
40
    public function __construct(Environment $twig, TranslatorInterface $translator, array $defaults)
41
    {
42
        $this->twig = $twig;
43
        $this->translator = $translator;
44
        $this->defaults = $defaults;
45
    }
46
47
    /**
48
     * @param array $options
49
     */
50
    public function configure($options)
51
    {
52
        $resolver = (new OptionsResolver)
53
            ->setRequired(['template'])
54
            ->setDefault('subject', '')
55
            ->setDefault('translation_catalog', '')
56
            ->setDefault('subject_parameters', [])
57
            ->setDefault('template_parameters', [])
58
            ->setDefault('template_vars', [])
59
            ->setNormalizer('subject_parameters', function ($opts, $value) {
60
                return array_values((array) $value);
61
            })
62
            ->setNormalizer('template_parameters', function ($opts, $value) {
63
                return array_values((array) $value);
64
            })
65
            ->setNormalizer('template_vars', function ($opts, $value) {
66
                return (array) $value;
67
            })
68
        ;
69
70
        foreach ($resolver->getDefinedOptions() as $option) {
71
            if (isset($this->defaults[$option])) {
72
                $resolver->setDefault($option, $this->defaults[$option]);
73
            }
74
        }
75
76
        $options = array_intersect_key(
77
            $options,
78
            array_flip($resolver->getDefinedOptions())
79
        );
80
81
        $this->options = $resolver->resolve($options);
82
    }
83
84
    /**
85
     * @param array $parameters
86
     *
87
     * @return string
88
     */
89
    public function getSubject(array $parameters)
90
    {
91
        if (null === $this->options) {
92
            throw BadMethodCallException::createMissingCall(
93
                __CLASS__.'::configure',
94
                __METHOD__
95
            );
96
        }
97
98
        return $this->translator->trans(
99
            $this->options['subject'],
100
            array_intersect_key($parameters, array_flip($this->options['subject_parameters'])),
101
            $this->options['translation_catalog']
102
        );
103
    }
104
105
    /**
106
     * @param array $parameters
107
     *
108
     * @return string
109
     */
110
    public function getBody(array $parameters)
111
    {
112
        if (null === $this->options) {
113
            throw BadMethodCallException::createMissingCall(
114
                __CLASS__.'::configure',
115
                __METHOD__
116
            );
117
        }
118
119
        return $this->twig->render(
120
            strtr(
121
                $this->options['template'],
122
                array_intersect_key($parameters, array_flip($this->options['template_parameters']))
123
            ),
124
            array_merge($parameters, $this->options['template_vars'])
125
        );
126
    }
127
}
128