BlogArticlesCommentsTable::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
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');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
        $this->displayField('comment');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
    public function validationCreate(Validator $validator)
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)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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