Callbacks::createFromPost()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 29

Duplication

Lines 8
Ratio 27.59 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 8
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
use callbacks\Exceptions\ValidationException;
4
use cmsemail\email;
5
use CMSFactory\assetManager;
6
use CMSFactory\Events;
7
use Propel\Runtime\ActiveQuery\Criteria;
8
use Propel\Runtime\Exception\PropelException;
9
10
(defined('BASEPATH')) OR exit('No direct script access allowed');
11
12
/**
13
 * Image CMS
14
 * Callbacks
15
 */
16
class Callbacks extends MY_Controller
17
{
18
19
    public function __construct() {
20
21
        parent::__construct();
22
        $lang = new MY_Lang();
23
        $lang->load('callbacks');
24
    }
25
26
    /**
27
     * Render form and save callback
28
     *
29
     * @return void
30
     * @throws PropelException
31
     */
32
    public function index() {
33
34
        $this->core->set_meta_tags(lang('Callback', 'callback'));
35
        $this->template->registerMeta('ROBOTS', 'NOINDEX, NOFOLLOW');
36
37
        $this->load->library('Form_validation');
38
        if ($this->input->post()) {
39
            try {
40
                $success = $this->createFromPost();
41
            } catch (ValidationException $e) {
42
                $success = false;
43
            }
44
            if (!$this->input->is_ajax_request()) {
45
                $this->session->set_flashdata('success_message', $success);
46
                redirect(site_url('/callbacks'));
47
            }
48
        }
49
50
        $message = isset($success) ? $success : $this->session->flashdata('success_message');
51
52
        assetManager::create()
53
            ->setData('themes', SCallbackThemesQuery::create()->setComment(__METHOD__)->joinWithI18n(MY_Controller::getCurrentLocale(), Criteria::INNER_JOIN)->find())
54
            ->setData(['success' => $message])
55
            ->render('callback');
56
    }
57
58
    /**
59
     * Create new callback from $_POST data
60
     * @return string
61
     * @throws ValidationException
62
     * @throws PropelException
63
     */
64
    public function createFromPost() {
65
66
        $this->load->library('Form_validation');
67
68
        $model = new SCallbacks;
69
        $this->form_validation->set_rules($model->rules());
70
71 View Code Duplication
        if (!$this->form_validation->run()) {
72
            throw new ValidationException(
73
                [
0 ignored issues
show
Documentation introduced by
array('message' => valid...tion->getErrorsArray()) is of type array<string,?,{"message":"?","errors":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
                 'message' => validation_errors(),
75
                 'errors'  => $this->form_validation->getErrorsArray(),
76
                ]
77
            );
78
        }
79
80
        $theme = SCallbackThemesQuery::create()->setComment(__METHOD__)->orderByPosition()->findOne();
81
        $status = SCallbackStatusesQuery::create()->setComment(__METHOD__)->filterByIsDefault(TRUE)->findOne();
82
83
        $model->fromArray($this->input->post());
84
        $model->setThemeId($theme ? $theme->getId() : 0);
85
        $model->setStatusId($status ? $status->getId() : 0);
86
        $model->setDate(time());
87
        $model->save();
88
89
        $this->sendEmail($model);
90
        Events::create()->raiseEvent(['model' => $model], 'Shop:callback');
91
        return $this->getMessage();
92
    }
93
94
    /**
95
     * @todo move callback configs(success message etc.)
96
     *       from answer notifications && shop settings
97
     *       to own module configs
98
     *
99
     * @return string
100
     */
101
    protected function getMessage() {
102
103
        $notification = $this->db
104
            ->where('locale', \MY_Controller::getCurrentLocale())
105
            ->where('name', 'callback')->get('answer_notifications');
106
107
        return $notification->num_rows() > 0 ? $notification->row()->message : '';
108
    }
109
110
    /**
111
     * @param SCallbacks $callback
112
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
113
     * @throws PropelException
114
     */
115
    protected function sendEmail(SCallbacks $callback) {
116
117
        $callback_variables = [
118
                               'callbackStatus' => $callback->getSCallbackStatuses() ? $callback->getSCallbackStatuses()->getText() : '',
119
                               'callbackTheme'  => $callback->getSCallbackThemes() ? $callback->getSCallbackThemes()->getText() : '',
120
                               'userName'       => $callback->getName(),
121
                               'userPhone'      => $callback->getPhone(),
122
                               'dateCreated'    => date('d-m-Y H:i:s', $callback->getDate()),
123
                               'userComment'    => $callback->getComment(),
124
                              ];
125
        return email::getInstance()->sendEmail($this->dx_auth->get_user_email(), 'callback', $callback_variables);
126
    }
127
128
}
129
130
/* End of file callbacks.php */