diff --git a/api/controllers/FeedbackController.php b/api/controllers/FeedbackController.php new file mode 100644 index 0000000..e185c6d --- /dev/null +++ b/api/controllers/FeedbackController.php @@ -0,0 +1,39 @@ + [ + 'except' => ['index'], + ], + ]); + } + + public function verbs() { + return [ + 'index' => ['POST'], + ]; + } + + public function actionIndex() { + $model = new FeedbackForm(); + $model->load(Yii::$app->request->post()); + if (!$model->sendMessage()) { + return [ + 'success' => false, + 'errors' => $this->normalizeModelErrors($model->getErrors()), + ]; + } + + return [ + 'success' => true, + ]; + } + +} diff --git a/api/mails/feedback.php b/api/mails/feedback.php new file mode 100644 index 0000000..92f75ff --- /dev/null +++ b/api/mails/feedback.php @@ -0,0 +1,29 @@ + + +

Форма обратной связи Account Ely.by

+
+
+
+Письмо отправлено:
+E-mail отправителя: email ?>
+ + Ник отправителя: username ?>
+ email !== $model->email) { ?> + Регистрационный E-mail: email ?>
+ + id; ?> + Ссылка на профиль:
+ Дата регистрации: created_at) ?>
+ +Тема: formatter->asText($model->subject) ?>
+Текст:
+formatter->asNtext($model->message) ?> diff --git a/api/models/FeedbackForm.php b/api/models/FeedbackForm.php new file mode 100644 index 0000000..f9f1361 --- /dev/null +++ b/api/models/FeedbackForm.php @@ -0,0 +1,65 @@ + 'error.{attribute}_required'], + [['subject'], 'string', 'max' => 255], + [['email'], 'email'], + [['message'], 'string', 'max' => 65535], + ]; + } + + public function sendMessage() : bool { + if (!$this->validate()) { + return false; + } + + /** @var \yii\swiftmailer\Mailer $mailer */ + $mailer = Yii::$app->mailer; + $fromEmail = Yii::$app->params['supportEmail']; + if (!$fromEmail) { + throw new InvalidConfigException('Please specify supportEmail value in app params'); + } + + $account = $this->getAccount(); + /** @var \yii\swiftmailer\Message $message */ + $message = $mailer->compose('@app/mails/feedback', [ + 'model' => $this, + 'account' => $account, + ]); + $message + ->setTo($this->email) + ->setFrom([$this->email => $account ? $account->username : $this->email]) + ->setSubject($this->subject); + + if (!$message->send()) { + throw new ErrorException('Unable send feedback email.'); + } + + return true; + } + + /** + * @return \common\models\Account|null $account + */ + protected function getAccount() { + return Yii::$app->user->identity; + } + +} diff --git a/environments/dev/common/config/params-local.php b/environments/dev/common/config/params-local.php index c573d3a..525df3a 100644 --- a/environments/dev/common/config/params-local.php +++ b/environments/dev/common/config/params-local.php @@ -1,4 +1,5 @@ 'account@ely.by', + 'supportEmail' => 'support@ely.by', ]; diff --git a/environments/docker/common/config/params-local.php b/environments/docker/common/config/params-local.php index c573d3a..525df3a 100644 --- a/environments/docker/common/config/params-local.php +++ b/environments/docker/common/config/params-local.php @@ -1,4 +1,5 @@ 'account@ely.by', + 'supportEmail' => 'support@ely.by', ]; diff --git a/environments/prod/common/config/params-local.php b/environments/prod/common/config/params-local.php index c573d3a..525df3a 100644 --- a/environments/prod/common/config/params-local.php +++ b/environments/prod/common/config/params-local.php @@ -1,4 +1,5 @@ 'account@ely.by', + 'supportEmail' => 'support@ely.by', ]; diff --git a/tests/codeception/api/unit/models/FeedbackFormTest.php b/tests/codeception/api/unit/models/FeedbackFormTest.php new file mode 100644 index 0000000..82a5409 --- /dev/null +++ b/tests/codeception/api/unit/models/FeedbackFormTest.php @@ -0,0 +1,77 @@ +mailer; + $mailer->fileTransportCallback = function() { + return self::FILE_NAME; + }; + } + + protected function tearDown() { + if (file_exists($this->getMessageFile())) { + unlink($this->getMessageFile()); + } + + parent::tearDown(); + } + + public function testSendMessage() { + $this->specify('send email', function() { + $model = new FeedbackForm([ + 'subject' => 'Тема обращения', + 'email' => 'erickskrauch@ely.by', + 'message' => 'Привет мир!', + ]); + expect($model->sendMessage())->true(); + expect_file('message file exists', $this->getMessageFile())->exists(); + }); + + $this->specify('send email with user info', function() { + /** @var FeedbackForm|\PHPUnit_Framework_MockObject_MockObject $model */ + $model = $this->getMockBuilder(FeedbackForm::class) + ->setMethods(['getAccount']) + ->setConstructorArgs([[ + 'subject' => 'Тема обращения', + 'email' => 'erickskrauch@ely.by', + 'message' => 'Привет мир!', + ]]) + ->getMock(); + + $model + ->expects($this->any()) + ->method('getAccount') + ->will($this->returnValue(new Account([ + 'id' => '123', + 'username' => 'Erick', + 'email' => 'find-this@email.net', + 'created_at' => time() - 86400, + ]))); + expect($model->sendMessage())->true(); + expect_file('message file exists', $this->getMessageFile())->exists(); + $data = file_get_contents($this->getMessageFile()); + expect(strpos($data, 'find-this@email.net'))->notEquals(false); + }); + } + + private function getMessageFile() { + /** @var \yii\swiftmailer\Mailer $mailer */ + $mailer = Yii::$app->mailer; + + return Yii::getAlias($mailer->fileTransportPath) . '/' . self::FILE_NAME; + } + +}