These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace App\Model\Table; |
||
3 | |||
4 | use ArrayObject; |
||
5 | use Cake\Event\Event; |
||
6 | use Cake\ORM\Entity; |
||
7 | use Cake\ORM\Table; |
||
8 | use Cake\Validation\Validator; |
||
9 | |||
10 | class BlogArticlesCommentsTable extends Table |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Initialize method |
||
15 | * |
||
16 | * @param array $config The configuration for the Table. |
||
17 | * |
||
18 | * @return void |
||
19 | */ |
||
20 | public function initialize(array $config) |
||
21 | { |
||
22 | $this->table('blog_articles_comments'); |
||
23 | $this->displayField('comment'); |
||
24 | $this->primaryKey('id'); |
||
25 | |||
26 | $this->addBehavior('Timestamp'); |
||
27 | $this->addBehavior('CounterCache', [ |
||
28 | 'Users' => ['blog_articles_comment_count'], |
||
29 | 'BlogArticles' => ['comment_count'] |
||
30 | ]); |
||
31 | |||
32 | $this->belongsTo('BlogArticles', [ |
||
33 | 'foreignKey' => 'article_id', |
||
34 | ]); |
||
35 | $this->belongsTo('Users', [ |
||
36 | 'foreignKey' => 'user_id', |
||
37 | ]); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Create validation rules. |
||
42 | * |
||
43 | * @param \Cake\Validation\Validator $validator Instance of the validator. |
||
44 | * |
||
45 | * @return \Cake\Validation\Validator |
||
46 | */ |
||
47 | View Code Duplication | public function validationCreate(Validator $validator) |
|
0 ignored issues
–
show
|
|||
48 | { |
||
49 | $validator |
||
50 | ->notEmpty('content') |
||
51 | ->add('content', [ |
||
52 | 'minLength' => [ |
||
53 | 'rule' => ['minLength', 5], |
||
54 | 'message' => __("Please, {0} characters minimum for your comment.", 5) |
||
55 | ] |
||
56 | ]); |
||
57 | |||
58 | return $validator; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * AfterSave callback. |
||
63 | * |
||
64 | * @param \Cake\Event\Event $event The afterSave event that was fired. |
||
65 | * @param \Cake\ORM\Entity $entity The entity that was saved. |
||
66 | * @param \ArrayObject $options The options passed to the callback. |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function afterSave(Event $event, Entity $entity, ArrayObject $options) |
||
71 | { |
||
72 | if ($entity->isNew()) { |
||
73 | $comment = new Event('Model.BlogArticlesComments.add', $this, [ |
||
74 | 'comment' => $entity |
||
75 | ]); |
||
76 | $this->eventManager()->dispatch($comment); |
||
77 | } |
||
78 | |||
79 | return true; |
||
80 | } |
||
81 | } |
||
82 |
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.