Passed
Pull Request — master (#1317)
by Dante
01:44
created

SendMailController::index()   A

Complexity

Conditions 4
Paths 21

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 21
nop 0
dl 0
loc 19
rs 9.7666
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Controller;
5
6
use BEdita\WebTools\ApiClientProvider;
7
use Cake\Utility\Hash;
8
use Exception;
9
10
/**
11
 * SendMail Controller
12
 */
13
class SendMailController extends AppController
14
{
15
    /**
16
     * @inheritDoc
17
     */
18
    public function initialize(): void
19
    {
20
        parent::initialize();
21
        $this->FormProtection->setConfig('unlockedActions', ['index']);
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function index(): void
28
    {
29
        $this->getRequest()->allowMethod(['post']);
30
        $this->viewBuilder()->setClassName('Json');
31
        try {
32
            $payload = $this->getRequest()->getData();
33
            foreach ($payload['data'] as $key => $value) {
34
                if (strpos($key, '.') !== false) {
35
                    unset($payload['data'][$key]);
36
                    $payload['data'] = Hash::insert($payload['data'], $key, $value);
37
                }
38
            }
39
            ApiClientProvider::getApiClient()->post('/placeholders/send', (string)json_encode($payload));
40
            $response = ['message' => 'Email sent successfully'];
41
            $this->set('response', $response);
42
            $this->setSerialize(['response']);
43
        } catch (Exception $e) {
44
            $this->set('error', $e->getMessage());
45
            $this->setSerialize(['error']);
46
        }
47
    }
48
}
49