Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ContractTemplateHelper
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 getReplacementStrings
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getReplacements
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
1
 replaceContent
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Service;
6
7use App\Entity\Contract;
8use App\Helper\IntlConverter;
9use App\Type\Gender;
10use IntlNumbersToWords\Numbers;
11
12class ContractTemplateHelper
13{
14    /**
15     * @return array<string>
16     */
17    public function getReplacementStrings(): array
18    {
19        $replacements = $this->getReplacements(new Contract()->setGender(Gender::other));
20        return array_keys($replacements);
21    }
22
23    /**
24     * @return array<string, string>
25     */
26    private function getReplacements(Contract $contract): array
27    {
28        $numberToWord = new Numbers();
29        return [
30            'local_no' => (string)$contract->getStoreNumber(),
31            'destination' => (string)$contract->getDestination(),
32            'val_alq' => number_format((float)$contract->getValAlq(), 2),
33            'txt_alq' => $numberToWord->toCurrency(
34                (float)$contract->getValAlq(),
35                'es_EC',
36                'USD'
37            ),
38            'val_garantia' => number_format(
39                (float)$contract->getValGarantia(),
40                2
41            ),
42            'txt_garantia' => $numberToWord->toCurrency(
43                (float)$contract->getValGarantia(),
44                'es_EC',
45                'USD'
46            ),
47            'fecha_long' => IntlConverter::formatDate($contract->getDate()),
48
49            'inq_nombreapellido' => (string)$contract->getInqNombreapellido(),
50            'inq_ci' => $contract->getInqCi(),
51
52            'senor_a' => $contract->getGender()->titleLong(),
53            'el_la' => $contract->getGender()->text_1(),
54            'del_la' => $contract->getGender()->text_2(),
55
56            'cnt_lanfort' => (string)$contract->getCntLanfort(),
57            'cnt_neon' => (string)$contract->getCntNeon(),
58            'cnt_switch' => (string)$contract->getCntSwitch(),
59            'cnt_toma' => (string)$contract->getCntToma(),
60            'cnt_ventana' => (string)$contract->getCntVentana(),
61            'cnt_llaves' => (string)$contract->getCntLlaves(),
62            'cnt_med_agua' => (string)$contract->getCntMedAgua(),
63            'cnt_med_elec' => (string)$contract->getCntMedElec(),
64
65            'med_electrico' => $contract->getMedElectrico(),
66            'med_agua' => $contract->getMedAgua(),
67        ];
68    }
69
70    public function replaceContent(Contract $contract): string
71    {
72        $replacements = $this->getReplacements($contract);
73        $start = '[';
74        $end = ']';
75        $search = array_map(static fn(string $item): string => $start.$item.$end, array_keys($replacements));
76        return str_replace(
77            $search,
78            $replacements,
79            $contract->getText()
80        );
81    }
82}