Passed
Push — master ( f2c1fd...66552f )
by Mihail
04:43
created

CommentAnswerAdd::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Apps\Model\Api\Comments;
4
5
6
use Apps\ActiveRecord\CommentAnswer;
7
use Apps\ActiveRecord\CommentPost;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\Model;
10
use Ffcms\Core\Exception\JsonException;
11
use Ffcms\Core\Helper\Date;
12
use Ffcms\Core\Helper\Type\Str;
13
14
/**
15
 * Class CommentAnswerAdd. Model to add comment answer to database and build response active record row.
16
 * @package Apps\Model\Api\Comments
17
 */
18
class CommentAnswerAdd extends Model
19
{
20
    public $message;
21
    public $replayTo = 0;
22
    public $guestName;
23
    public $ip;
24
25
    private $_configs;
26
    private $_userId = 0;
27
28
    public function __construct(array $configs)
29
    {
30
        $this->_configs = $configs;
31
        parent::__construct();
32
    }
33
34 View Code Duplication
    public function before()
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...
35
    {
36
        $this->ip = App::$Request->getClientIp();
37
        if (App::$User->isAuth()) {
38
            $this->_userId = App::$User->identity()->getId();
39
        }
40
    }
41
42
    /**
43
     * Check if comment answer conditions is ok. Will throw exception if not.
44
     * @return bool
45
     * @throws JsonException
46
     */
47
    public function check()
48
    {
49
        // check if user is auth'd or guest name is defined
50 View Code Duplication
        if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            throw new JsonException(__('Guest name is not defined'));
52
        }
53
54
        // guest moderation
55 View Code Duplication
        if (!App::$User->isAuth() && (bool)$this->_configs['guestModerate']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            $captcha = App::$Request->request->get('captcha');
57
            if (!App::$Captcha->validate($captcha)) {
58
                throw new JsonException(__('Captcha is incorrect! Click on image to refresh and try again'));
59
            }
60
        }
61
62
        // check if replayTo is defined
63
        if ($this->replayTo < 1) {
64
            throw new JsonException(__('Comment post thread is not founded'));
65
        }
66
67
        // check if message length is correct
68 View Code Duplication
        if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            throw new JsonException(__('Message length is incorrect. Current: %cur%, min - %min%, max - %max%', [
70
                'cur' => Str::length($this->message),
71
                'min' => $this->_configs['minLength'],
72
                'max' => $this->_configs['maxLength']
73
            ]));
74
        }
75
76
        $count = CommentPost::where('id', '=', $this->replayTo)->count();
77
        if ($count !== 1) {
78
            throw new JsonException(__('Comment post thread is not founded'));
79
        }
80
81
        // check to prevent spam
82
        $query = CommentAnswer::where('user_id', '=', $this->_userId)
83
            ->orWhere('ip', '=', $this->ip)
84
            ->orderBy('created_at', 'DESC')
85
            ->first();
86
87
        // something is founded :D
88 View Code Duplication
        if ($query !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            $answerTime = Date::convertToTimestamp($query->created_at);
90
            $delay = $answerTime + $this->_configs['delay'] - time();
91
            if ($delay > 0) { // sounds like config time is not passed now
92
                throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay]));
93
            }
94
        }
95
96
        return true;
97
    }
98
99
    /**
100
     * Add comment answer to database and return active record object
101
     * @return CommentAnswer
102
     */
103 View Code Duplication
    public function buildRecord()
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...
104
    {
105
        $record = new CommentAnswer();
106
        $record->comment_id = $this->replayTo;
107
        $record->user_id = $this->_userId;
108
        $record->guest_name = $this->guestName;
109
        $record->message = $this->message;
110
        $record->lang = App::$Request->getLanguage();
111
        $record->ip = $this->ip;
112
        // check if premoderation is enabled and user is guest
113
        if ((int)$this->_configs['guestModerate'] === 1 && $this->_userId < 1) {
114
            $record->moderate = 1;
115
        }
116
        $record->save();
117
118
        return $record;
119
    }
120
121
}