Passed
Push — master ( 8f5376...4142cd )
by Mihail
12:41
created

CommentPostAdd   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 8
dl 38
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A before() 9 9 2
D check() 17 38 9
A buildRecord() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Apps\Model\Api\Comments;
4
5
6
use Apps\ActiveRecord\CommentPost;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Exception\JsonException;
10
use Ffcms\Core\Helper\Date;
11
use Ffcms\Core\Helper\Type\Str;
12
13
/**
14
 * Class CommentPostAdd. Model to parse and insert input comment post data.
15
 * @package Apps\Model\Api\Comments
16
 */
17
class CommentPostAdd extends Model
18
{
19
    public $pathway;
20
    public $message;
21
    public $guestName;
22
23
    public $ip;
24
25
    private $_configs;
26
    private $_userId = 0;
27
28
    /**
29
     * CommentPostAdd constructor. Pass configuration inside.
30
     * @param array $configs
31
     */
32
    public function __construct(array $configs)
33
    {
34
        $this->_configs = $configs;
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Prepare model data - user ip and other data
40
     */
41 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...
42
    {
43
        // set user ip
44
        $this->ip = App::$Request->getClientIp();
45
        // set user object if auth done
46
        if (App::$User->isAuth()) {
47
            $this->_userId = App::$User->identity()->getId();
48
        }
49
    }
50
51
    /**
52
     * Check comment add conditions. On bad conditions will be throw'd exception.
53
     * @throws JsonException
54
     * @return boolean
55
     */
56
    public function check()
57
    {
58
        // check if user is auth'd or guest name is defined
59 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...
60
            throw new JsonException(__('Guest name is not defined'));
61
        }
62
63
        // check if pathway is empty
64
        if (Str::likeEmpty($this->pathway)) {
65
            throw new JsonException(__('Wrong target pathway'));
66
        }
67
68
        // check if message length is correct
69 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...
70
            throw new JsonException(__('Message length is incorrect. Current: %cur% , min - %min%, max - %max%', [
71
                'cur' => Str::length($this->message),
72
                'min' => $this->_configs['minLength'],
73
                'max' => $this->_configs['maxLength']
74
            ]));
75
        }
76
77
        // check delay between 2 comments from 1 user or 1 ip
78
        $query = CommentPost::where('user_id', '=', $this->_userId)
79
            ->orWhere('ip', '=', $this->ip)
80
            ->orderBy('created_at', 'DESC')
81
            ->first();
82
83
        // check if latest post time for this user is founded
84 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...
85
            $postTime = Date::convertToTimestamp($query->created_at);
86
            $delay = $postTime + $this->_configs['delay'] - time();
87
            if ($delay > 0) {
88
                throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay]));
89
            }
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * Insert new comment in table and return active record object
97
     * @return CommentPost
98
     */
99 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...
100
    {
101
        $record = new CommentPost();
102
        $record->pathway = $this->pathway;
103
        $record->user_id = $this->_userId;
104
        $record->guest_name = $this->guestName;
105
        $record->message = $this->message;
106
        $record->lang = App::$Request->getLanguage();
107
        $record->save();
108
109
        return $record;
110
    }
111
}