Skip to content

Commit a0a03e4

Browse files
author
igor
committed
remove User object from CommentEvent, update DefaultController
1 parent 0ef986e commit a0a03e4

File tree

5 files changed

+36
-54
lines changed

5 files changed

+36
-54
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ To access the module, you need to add the following code to your application con
5050
'default' => [
5151
'class' => 'yii2mod\comments\controllers\DefaultController',
5252
'on afterCreate' => function ($event) {
53-
// get userId
54-
$event->getUser()->getId();
55-
// get identity
56-
$event->getUser()->getIdentity();
5753
// get saved comment model
5854
$event->getCommentModel();
5955
// your custom code

assets/js/comment.js

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
}
8888
$commentForm.find(':submit').prop('disabled', false).text(settings.submitBtnText);
8989
}
90+
}).fail(function (xhr, status, error) {
91+
alert(error);
9092
});
9193

9294
return false;

controllers/DefaultController.php

+32-26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yii;
66
use yii\filters\VerbFilter;
77
use yii\helpers\Json;
8+
use yii\web\BadRequestHttpException;
89
use yii\web\Controller;
910
use yii\web\NotFoundHttpException;
1011
use yii\web\Response;
@@ -54,36 +55,23 @@ public function behaviors()
5455
* Create comment.
5556
*
5657
* @param $entity string encrypt entity
57-
* @return array|null|Response
58+
* @return array
5859
*/
5960
public function actionCreate($entity)
6061
{
61-
/* @var $module Module */
62-
$module = Yii::$app->getModule(Module::$name);
63-
$event = Yii::createObject(['class' => CommentEvent::className(), 'user' => Yii::$app->user]);
64-
$commentModelClass = $module->commentModelClass;
65-
$decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8_decode($entity), $module::$name);
66-
if ($decryptEntity !== false) {
67-
$entityData = Json::decode($decryptEntity);
68-
/* @var $model CommentModel */
69-
$model = new $commentModelClass;
70-
$model->setAttributes($entityData);
71-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
72-
$event->setCommentModel($model);
73-
$this->trigger(self::EVENT_AFTER_CREATE, $event);
74-
return ['status' => 'success'];
75-
} else {
76-
return [
77-
'status' => 'error',
78-
'errors' => ActiveForm::validate($model)
79-
];
80-
}
81-
} else {
82-
return [
83-
'status' => 'error',
84-
'message' => Yii::t('yii2mod.comments', 'Oops, something went wrong. Please try again later.')
85-
];
62+
$commentModel = Yii::createObject(Yii::$app->getModule(Module::$name)->commentModelClass);
63+
$commentModel->setAttributes($this->getCommentAttributesFromEntity($entity));
64+
if ($commentModel->load(Yii::$app->request->post()) && $commentModel->save()) {
65+
$event = Yii::createObject(['class' => CommentEvent::className(), 'commentModel' => $commentModel]);
66+
$this->trigger(self::EVENT_AFTER_CREATE, $event);
67+
68+
return ['status' => 'success'];
8669
}
70+
71+
return [
72+
'status' => 'error',
73+
'errors' => ActiveForm::validate($commentModel)
74+
];
8775
}
8876

8977
/**
@@ -119,4 +107,22 @@ protected function findModel($id)
119107
throw new NotFoundHttpException(Yii::t('yii2mod.comments', 'The requested page does not exist.'));
120108
}
121109
}
110+
111+
/**
112+
* Get list of attributes from encrypted entity
113+
*
114+
* @param $entity string encrypted entity
115+
* @return array|mixed
116+
*
117+
* @throws BadRequestHttpException
118+
*/
119+
protected function getCommentAttributesFromEntity($entity)
120+
{
121+
$decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8_decode($entity), Module::$name);
122+
if ($decryptEntity !== false) {
123+
return Json::decode($decryptEntity);
124+
}
125+
126+
throw new BadRequestHttpException(Yii::t('yii2mod.comments', 'Oops, something went wrong. Please try again later.'));
127+
}
122128
}

events/CommentEvent.php

-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace yii2mod\comments\events;
44

55
use yii\base\Event;
6-
use yii\web\User;
76
use yii2mod\comments\models\CommentModel;
87

98
/**
@@ -12,32 +11,11 @@
1211
*/
1312
class CommentEvent extends Event
1413
{
15-
/**
16-
* @var User
17-
*/
18-
private $_user;
19-
2014
/**
2115
* @var CommentModel
2216
*/
2317
private $_commentModel;
2418

25-
/**
26-
* @return User
27-
*/
28-
public function getUser()
29-
{
30-
return $this->_user;
31-
}
32-
33-
/**
34-
* @param User $user
35-
*/
36-
public function setUser(User $user)
37-
{
38-
$this->_user = $user;
39-
}
40-
4119
/**
4220
* @return CommentModel
4321
*/

tests/CommentTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function testDeleteComment()
3737

3838
public function testTryAddCommentWithInvalidEntityParam()
3939
{
40-
$response = Yii::$app->runAction('comment/default/create', ['entity' => 'invalid entity']);
41-
$this->assertEquals('error', $response['status'], 'Response status is invalid!');
40+
$this->setExpectedException('yii\web\BadRequestHttpException');
41+
Yii::$app->runAction('comment/default/create', ['entity' => 'invalid entity']);
4242
}
4343

4444
/**

0 commit comments

Comments
 (0)