EmailTemplatesController::onConstruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\Models\EmailTemplates;
8
use Canvas\Models\Users;
9
use Canvas\Http\Exception\NotFoundException;
10
use Phalcon\Security\Random;
0 ignored issues
show
Bug introduced by
The type Phalcon\Security\Random was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Phalcon\Http\Response;
0 ignored issues
show
Bug introduced by
The type Phalcon\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
14
15
/**
16
 * Class LanguagesController.
17
 *
18
 * @package Canvas\Api\Controllers
19
 *
20
 * @property Users $userData
21
 * @property Request $request
22
 * @property Config $config
23
 * @property \Baka\Mail\Message $mail
24
 * @property Apps $app
25
 *
26
 */
27
class EmailTemplatesController extends BaseController
28
{
29
    /*
30
     * fields we accept to create
31
     *
32
     * @var array
33
     */
34
    protected $createFields = [
35
        'users_id',
36
        'companies_id',
37
        'apps_id',
38
        'name',
39
        'template'
40
    ];
41
42
    /*
43
     * fields we accept to create
44
     *
45
     * @var array
46
     */
47
    protected $updateFields = [
48
        'users_id',
49
        'companies_id',
50
        'apps_id',
51
        'name',
52
        'template'
53
    ];
54
55
    /**
56
     * set objects.
57
     *
58
     * @return void
59
     */
60
    public function onConstruct()
61
    {
62
        $this->model = new EmailTemplates();
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
        $this->additionalSearchFields = [
0 ignored issues
show
Bug Best Practice introduced by
The property additionalSearchFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
            ['is_deleted', ':', '0'],
65
            ['companies_id', ':', '0|' . $this->userData->currentCompanyId()],
66
        ];
67
    }
68
69
    /**
70
     * Add a new by copying a specific email template based on.
71
     *
72
     * @method POST
73
     * @url /v1/data
74
     * @param integer $id
75
     * @return \Phalcon\Http\Response
76
     */
77
    public function copy(int $id): Response
78
    {
79
        $request = $this->request->getPostData();
80
81
        //Find email template based on the basic parameters
82
        $existingEmailTemplate = $this->model::findFirst([
83
            'conditions' => 'id = ?0 and companies_id in (?1,?2) and apps_id in (?3,?4) and is_deleted = 0',
84
            'bind' => [$id, $this->userData->currentCompanyId(), 0, $this->app->getId(), 0]
85
        ]);
86
87
        if (!is_object($existingEmailTemplate)) {
88
            throw new NotFoundException('Email Template not found');
89
        }
90
91
        $random = new Random();
92
        $randomInstance = $random->base58();
93
94
        $request['users_id'] = $existingEmailTemplate->users_id;
95
        $request['companies_id'] = $this->userData->currentCompanyId();
96
        $request['apps_id'] = $this->app->getId();
97
        $request['name'] = $existingEmailTemplate->name . '-' . $randomInstance;
98
        $request['template'] = $existingEmailTemplate->template;
99
100
        $this->model->saveOrFail($request, $this->createFields);
101
102
        return $this->response($this->model->toArray());
103
    }
104
105
    /**
106
     * Send test email to specific recipient.
107
     * @param string $email
108
     * @return Response
109
     */
110
    public function sendTestEmail(): Response
111
    {
112
        $request = $this->request->getPost();
113
114
        if (empty($request)) {
115
            $request = $this->request->getJsonRawBody(true);
116
        }
117
118
        $emailRecipients = explode(',', $request['emails']);
119
120
        foreach ($emailRecipients as $emailRecipient) {
121
            $userExists = Users::findFirst([
122
                'conditions' => 'email = ?0 and is_deleted = 0',
123
                'bind' => [$emailRecipient]
124
            ]);
125
126
            if (!is_object($userExists)) {
127
                throw new NotFoundException('Email recipient not found');
128
            }
129
130
            $subject = _('Test Email Template');
131
            $this->mail
132
                ->to((string)$userExists->email)
133
                ->subject($subject)
134
                ->content($request['template'])
135
                ->sendNow();
136
        }
137
138
        return $this->response('Test email sent');
139
    }
140
}
141