Passed
Push — master ( 68443e...939347 )
by Mihail
04:31
created

FormAnswerAdd::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Front\Feedback;
4
5
use Apps\ActiveRecord\FeedbackAnswer;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
9
/**
10
 * Class FormAnswerAdd. Model to work with add answer to feedback post thread
11
 * @package Apps\Model\Front\Feedback
12
 */
13
class FormAnswerAdd extends Model
14
{
15
    public $name;
16
    public $email;
17
    public $message;
18
19
    /** @var \Apps\ActiveRecord\FeedbackPost $_post */
20
    protected $_post;
21
    protected $_userId;
22
    protected $_ip;
23
24
    /**
25
     * FormAnswerAdd constructor. Pass active record of comment post and user id
26
     * @param $recordPost
27
     * @param int $userId
28
     */
29
    public function __construct($recordPost, $userId = 0)
30
    {
31
        $this->_post = $recordPost;
32
        $this->_userId = (int)$userId;
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Define local properties if user is authorized
38
     */
39
    public function before()
40
    {
41
        if ($this->_userId > 0) {
42
            $user = App::$User->identity($this->_userId);
43
            $this->name = $user->getProfile()->nick;
44
            $this->email = $user->getParam('email');
45
        }
46
        $this->_ip = App::$Request->getClientIp();
47
    }
48
49
    /**
50
    * Labels for display form
51
    */
52 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...
53
    {
54
        return [
55
            'name' => __('Name'),
56
            'email' => __('Email'),
57
            'message' => __('Message')
58
        ];
59
    }
60
61
    /**
62
    * Form validation rules
63
    */
64 View Code Duplication
    public function rules()
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...
65
    {
66
        return [
67
            [['name', 'email', 'message'], 'required'],
68
            ['name', 'length_min', 2],
69
            ['email', 'email'],
70
            ['message', 'length_min', 10]
71
        ];
72
    }
73
74
    /**
75
     * Add new row to database and set post is unreaded
76
     */
77 View Code Duplication
    public function make()
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...
78
    {
79
        // update readed marker
80
        $this->_post->readed = 0;
81
        $this->_post->save();
82
83
        // add new answer row in database
84
        $record = new FeedbackAnswer();
85
        $record->feedback_id = $this->_post->id;
86
        $record->name = App::$Security->strip_tags($this->name);
87
        $record->email = App::$Security->strip_tags($this->email);
88
        $record->message = App::$Security->strip_tags($this->message);
89
        if ($this->_userId > 0) {
90
            $record->user_id = $this->_userId;
91
        }
92
        $record->ip = $this->_ip;
93
        $record->save();
94
    }
95
}