Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.00% |
34 / 40 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CommentsTable | |
85.00% |
34 / 40 |
|
75.00% |
3 / 4 |
5.08 | |
0.00% |
0 / 1 |
| initialize | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| validationDefault | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| buildRules | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| afterSave | |
14.29% |
1 / 7 |
|
0.00% |
0 / 1 |
4.52 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Model\Table; |
| 5 | |
| 6 | use App\Utility\SettingsManager; |
| 7 | use ArrayObject; |
| 8 | use Cake\Datasource\EntityInterface; |
| 9 | use Cake\Event\EventInterface; |
| 10 | use Cake\Log\LogTrait; |
| 11 | use Cake\ORM\RulesChecker; |
| 12 | use Cake\ORM\Table; |
| 13 | use Cake\Validation\Validator; |
| 14 | |
| 15 | /** |
| 16 | * CommentsTable |
| 17 | * |
| 18 | * Represents the comments table in the database. Manages relationships with Users and Articles, |
| 19 | * defines validation rules, and sets up integrity checks for the comments data. |
| 20 | * |
| 21 | * @property \App\Model\Table\UsersTable&\Cake\ORM\Association\BelongsTo $Users |
| 22 | * @property \App\Model\Table\ArticlesTable&\Cake\ORM\Association\BelongsTo $Articles |
| 23 | * @method \App\Model\Entity\Comment newEmptyEntity() |
| 24 | * @method \App\Model\Entity\Comment newEntity(array $data, array $options = []) |
| 25 | * @method \App\Model\Entity\Comment get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) |
| 26 | * @method \App\Model\Entity\Comment|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) |
| 27 | * @mixin \Cake\ORM\Behavior\TimestampBehavior |
| 28 | */ |
| 29 | class CommentsTable extends Table |
| 30 | { |
| 31 | use LogTrait; |
| 32 | use QueueableJobsTrait; |
| 33 | |
| 34 | /** |
| 35 | * Initialize method |
| 36 | * |
| 37 | * @param array<string, mixed> $config The configuration for the Table. |
| 38 | * @return void |
| 39 | */ |
| 40 | public function initialize(array $config): void |
| 41 | { |
| 42 | parent::initialize($config); |
| 43 | |
| 44 | $this->setTable('comments'); |
| 45 | $this->setDisplayField('model'); |
| 46 | $this->setPrimaryKey('id'); |
| 47 | |
| 48 | $this->addBehavior('Timestamp'); |
| 49 | |
| 50 | $this->belongsTo('Users', [ |
| 51 | 'foreignKey' => 'user_id', |
| 52 | 'joinType' => 'INNER', |
| 53 | ]); |
| 54 | |
| 55 | $this->belongsTo('Articles', [ |
| 56 | 'foreignKey' => 'foreign_key', |
| 57 | 'conditions' => ['Comments.model' => 'Articles'], |
| 58 | 'joinType' => 'LEFT', |
| 59 | ]); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Default validation rules. |
| 64 | * |
| 65 | * @param \Cake\Validation\Validator $validator Validator instance. |
| 66 | * @return \Cake\Validation\Validator |
| 67 | */ |
| 68 | public function validationDefault(Validator $validator): Validator |
| 69 | { |
| 70 | $validator |
| 71 | ->uuid('foreign_key') |
| 72 | ->requirePresence('foreign_key', 'create') |
| 73 | ->notEmptyString('foreign_key'); |
| 74 | |
| 75 | $validator |
| 76 | ->scalar('model') |
| 77 | ->maxLength('model', 255) |
| 78 | ->requirePresence('model', 'create') |
| 79 | ->notEmptyString('model'); |
| 80 | |
| 81 | $validator |
| 82 | ->uuid('user_id') |
| 83 | ->notEmptyString('user_id'); |
| 84 | |
| 85 | $validator |
| 86 | ->scalar('content') |
| 87 | ->requirePresence('content', 'create') |
| 88 | ->notEmptyString('content'); |
| 89 | |
| 90 | return $validator; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns a rules checker object that will be used for validating |
| 95 | * application integrity. |
| 96 | * |
| 97 | * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. |
| 98 | * @return \Cake\ORM\RulesChecker |
| 99 | */ |
| 100 | public function buildRules(RulesChecker $rules): RulesChecker |
| 101 | { |
| 102 | $rules->add($rules->existsIn(['user_id'], 'Users'), ['errorField' => 'user_id']); |
| 103 | |
| 104 | return $rules; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * After save callback. |
| 109 | * |
| 110 | * This method is called after a comment entity is saved. If AI analysis is enabled, |
| 111 | * it queues up a job to analyze the comment content for inappropriate language. |
| 112 | * |
| 113 | * @param \Cake\Event\EventInterface $event The event that was fired |
| 114 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
| 115 | * @param \ArrayObject $options The options passed to the save method |
| 116 | * @return void |
| 117 | */ |
| 118 | public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void |
| 119 | { |
| 120 | if (SettingsManager::read('AI.enabled')) { |
| 121 | $data = [ |
| 122 | 'comment_id' => $entity->id, |
| 123 | 'content' => $entity->content, |
| 124 | 'user_id' => $entity->user_id, |
| 125 | ]; |
| 126 | |
| 127 | // Queue up a comment analysis job |
| 128 | $this->queueJob('App\Job\CommentAnalysisJob', $data); |
| 129 | } |
| 130 | } |
| 131 | } |