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

FormFeedbackAdd::rules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 10
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
        return $record;
91
    }
92
93
    /**
94
     * @return string|null
95
     */
96
    public function getHash()
97
    {
98
        return $this->_hash;
99
    }
100
}