Passed
Push — master ( 72ceee...7d97ae )
by Mihail
04:14
created

FormFeedbackAdd::make()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 13
nc 2
nop 0
1
<?php
2
3
namespace Apps\Model\Front\Feedback;
4
5
use Apps\ActiveRecord\FeedbackPost;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\Type\Str;
9
10
class FormFeedbackAdd extends Model
11
{
12
    public $name;
13
    public $email;
14
    public $captcha;
15
    public $message;
16
17
    private $_hash;
18
19
    private $_useCaptcha = true;
20
21
    public function __construct($captcha = true)
22
    {
23
        $this->_useCaptcha = (bool)$captcha;
24
        parent::__construct();
25
    }
26
27
28
    /**
29
    * Set user data if he is logged in
30
    */
31
    public function before()
32
    {
33
        if (App::$User->isAuth()) {
34
            $data = App::$User->identity();
35
            $this->name = $data->getProfile()->nick;
36
            $this->email = $data->email;
0 ignored issues
show
Bug introduced by
Accessing email on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
37
        }
38
    }
39
40
    /**
41
    * Labels to display form
42
    */
43 View Code Duplication
    public function labels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        return [
46
            'name' => __('Name'),
47
            'email' => __('Email'),
48
            'message' => __('Message'),
49
            'captcha' => __('Captcha')
50
        ];
51
    }
52
53
    /**
54
    * Rules to validate send form
55
    */
56
    public function rules()
57
    {
58
        $rules = [
59
            [['name', 'email', 'message'], 'required'],
60
            ['name', 'length_min', '2'],
61
            ['message', 'length_min', 10],
62
            ['email', 'email'],
63
            ['captcha', 'used']
64
        ];
65
        if (true === $this->_useCaptcha) {
66
            $rules[] = ['captcha', 'App::$Captcha::validate'];
67
        }
68
        return $rules;
69
    }
70
71
    public function make()
72
    {
73
        // calculate security hash to direct-on access
74
        $hash = Str::randomLatinNumeric(mt_rand(16, 64));
75
76
        // init new row and set row data
77
        $record = new FeedbackPost();
78
        $record->name = App::$Security->strip_tags($this->name);
79
        $record->email = App::$Security->strip_tags($this->email);
80
        $record->message = App::$Security->strip_tags($this->message);
81
        $record->hash = $hash;
82
        if (App::$User->isAuth()) {
83
            $record->user_id = App::$User->identity()->getId();
84
        }
85
86
        $record->ip = App::$Request->getClientIp();
87
        // save row to db
88
        $record->save();
89
90
        // send notification to email
91
        $this->sendEmail($record);
92
93
        return $record;
94
    }
95
96
    /**
97
     * Send email notification after feedback request creation
98
     * @param $record
99
     * @throws \Ffcms\Core\Exception\SyntaxException
100
     */
101
    private function sendEmail($record)
102
    {
103
        // prepare email template
104
        $template = App::$View->render('feedback/mail/created', [
105
            'record' => $record
106
        ]);
107
108
        // get website default email
109
        $sender = App::$Properties->get('adminEmail');
110
111
        // build swift mailer handler
112
        $mailMessage = \Swift_Message::newInstance(App::$Translate->get('Feedback', 'Request #%id% is created', ['id' => $record->id]))
113
            ->setFrom([$sender])
114
            ->setTo([$record->email])
115
            ->setBody($template, 'text/html');
116
        // send message over swift instance
117
        App::$Mailer->send($mailMessage);
118
    }
119
120
    /**
121
     * @return string|null
122
     */
123
    public function getHash()
124
    {
125
        return $this->_hash;
126
    }
127
}