Passed
Push — master ( 029921...577268 )
by Maximo
03:01
created

EmailTemplatesController::copy()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0629

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 6
nop 1
dl 0
loc 33
ccs 16
cts 19
cp 0.8421
crap 4.0629
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\EmailTemplates;
8
use Gewaer\Exception\NotFoundHttpException;
9
use Gewaer\Exception\UnprocessableEntityHttpException;
10
use Phalcon\Security\Random;
11
use Phalcon\Http\Response;
12
13
/**
14
 * Class LanguagesController
15
 *
16
 * @package Gewaer\Api\Controllers
17
 *
18
 * @property Users $userData
19
 * @property Request $request
20
 * @property Config $config
21
 * @property \Baka\Mail\Message $mail
22
 * @property Apps $app
23
 *
24
 */
25
class EmailTemplatesController extends BaseController
26
{
27
    /*
28
     * fields we accept to create
29
     *
30
     * @var array
31
     */
32
    protected $createFields = ['users_id', 'companies_id', 'apps_id', 'name', 'template'];
33
34
    /*
35
     * fields we accept to create
36
     *
37
     * @var array
38
     */
39
    protected $updateFields = ['users_id', 'companies_id', 'apps_id', 'name', 'template'];
40
41
    /**
42
     * set objects
43
     *
44
     * @return void
45
     */
46 2
    public function onConstruct()
47
    {
48 2
        $this->model = new EmailTemplates();
49 2
        $this->additionalSearchFields = [
50 2
            ['is_deleted', ':', '0'],
51 2
            ['companies_id', ':', '0|' . $this->userData->currentCompanyId()],
52
        ];
53 2
    }
54
55
    /**
56
     * Add a new by copying a specific email template based on
57
     *
58
     * @method POST
59
     * @url /v1/data
60
     * @param integer $id
61
     * @return \Phalcon\Http\Response
62
     */
63 1
    public function copy(int $id): Response
64
    {
65 1
        $request = $this->request->getPost();
66
67 1
        if (empty($request)) {
68
            $request = $this->request->getJsonRawBody(true);
69
        }
70
71
        //Find email template based on the basic parameters
72 1
        $existingEmailTemplate = $this->model::findFirst([
73 1
            'conditions' => 'id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted = 0',
74 1
            'bind' => [$id, $this->userData->default_company, $this->app->getId()]
75
        ]);
76
77 1
        if (!is_object($existingEmailTemplate)) {
78
            throw new NotFoundHttpException('Email Template not found');
79
        }
80
81 1
        $random = new Random();
82 1
        $randomInstance = $random->base58();
83
84 1
        $request['users_id'] = $existingEmailTemplate->users_id;
85 1
        $request['companies_id'] = $existingEmailTemplate->companies_id;
86 1
        $request['apps_id'] = $existingEmailTemplate->apps_id;
87 1
        $request['name'] = $existingEmailTemplate->name . '-' . $randomInstance;
88 1
        $request['template'] = $existingEmailTemplate->template;
89
90
        //try to save all the fields we allow
91 1
        if ($this->model->save($request, $this->createFields)) {
92 1
            return $this->response($this->model->toArray());
93
        } else {
94
            //if not thorw exception
95
            throw new UnprocessableEntityHttpException((string) current($this->model->getMessages()));
96
        }
97
    }
98
}
99