This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from PHPSP/develop
Atualizando master com a tela de contato
- Loading branch information
Showing
12 changed files
with
309 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/LigaSolidariaStorage/Storage/Controller/ContactController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace LigaSolidariaStorage\Storage\Controller; | ||
|
||
use Respect\Rest\Routable; | ||
use LigaSolidariaStorage\Storage\Mailer\Sender; | ||
use LigaSolidariaStorage\Storage\Mailer\Message; | ||
use LigaSolidariaStorage\Storage\Exception\InvalidMessageException; | ||
|
||
class ContactController implements Routable | ||
{ | ||
const TEMPLATE = 'contact.html.twig'; | ||
|
||
protected $sender; | ||
|
||
protected $receiverMail; | ||
|
||
protected $receiverName; | ||
|
||
protected $subject; | ||
|
||
public function __construct($config) | ||
{ | ||
$smtp = $config['smtp']; | ||
$port = $config['port']; | ||
$ssl = $config['ssl'] ? 'ssl' : null; | ||
$swiftTransport = \Swift_SmtpTransport::newInstance($smtp, $port, $ssl); | ||
$swiftTransport->setUsername($config['username']); | ||
$swiftTransport->setPassword($config['password']); | ||
$swiftSender = \Swift_Mailer::newInstance($swiftTransport); | ||
|
||
$this->receiverMail = $config['receiver_mail']; | ||
$this->receiverName = $config['receiver_name']; | ||
$this->subject = $config['subject']; | ||
$this->sender = new Sender($swiftSender); | ||
} | ||
|
||
public function get() | ||
{ | ||
return array( | ||
'_view' => self::TEMPLATE | ||
); | ||
} | ||
|
||
public function post() | ||
{ | ||
$message = new Message(); | ||
try { | ||
|
||
$message->setSubject($this->subject.' : '.$_POST['reason']); | ||
$message->setFrom(array($_POST['email'] => $_POST['name'])); | ||
$message->setTo(array($this->receiverMail)); | ||
$message->setBody($_POST['message']); | ||
|
||
$sent = $this->sender->send($message); | ||
} catch (\Swift_SwiftException $e) { | ||
return $this->getErrorResponse(array($e->getMessage())); | ||
} catch (InvalidMessageException $e) { | ||
return $this->getErrorResponse(array($e->getMessage())); | ||
} | ||
|
||
if (!$sent) { | ||
return $this->getErrorResponse(array('E-mail não enviado')); | ||
} | ||
$response = array( | ||
'_view' => self::TEMPLATE, | ||
'success' => 'Email enviado com sucesso!', | ||
); | ||
|
||
return $response; | ||
} | ||
|
||
protected function getErrorResponse($messages) | ||
{ | ||
header('HTTP/1.1 406'); | ||
$response = array( | ||
'_view' => self::TEMPLATE , | ||
'errors' => $messages | ||
); | ||
|
||
return $response; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/LigaSolidariaStorage/Storage/Exception/InvalidMessageException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace LigaSolidariaStorage\Storage\Exception; | ||
|
||
class InvalidMessageException extends \Exception{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace LigaSolidariaStorage\Storage\Mailer; | ||
|
||
class Message extends \Swift_Message implements MessageInterface | ||
{ | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/LigaSolidariaStorage/Storage/Mailer/MessageInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace LigaSolidariaStorage\Storage\Mailer; | ||
|
||
interface MessageInterface | ||
{ | ||
public function setFrom($addresses, $name = null); | ||
|
||
public function setBody($body); | ||
|
||
public function setTo($addresses, $name = null); | ||
|
||
public function setSubject($subject); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace LigaSolidariaStorage\Storage\Mailer; | ||
|
||
use LigaSolidariaStorage\Storage\Exception\InvalidMessageException; | ||
use Respect\Validation\Validator as v; | ||
|
||
class Sender | ||
{ | ||
protected $swiftSender; | ||
|
||
public function __construct($sender) | ||
{ | ||
$this->swiftSender = $sender; | ||
} | ||
|
||
public function send(MessageInterface $message) | ||
{ | ||
$this->validateMessage($message); | ||
return $this->swiftSender->send($message); | ||
} | ||
|
||
protected function validateMessage(MessageInterface $message) | ||
{ | ||
if (!$message->getFrom()) { | ||
throw new InvalidMessageException('E-mail de contato vazio'); | ||
} | ||
|
||
$text = trim($message->getBody()); | ||
$bodyValidation = v::string()->notEmpty(); | ||
if (!$bodyValidation->validate($text)) { | ||
throw new InvalidMessageException('Mensagem vazia'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{% extends "layouts/layout.html.twig" %} | ||
|
||
{% block title %}Contato{% endblock %} | ||
|
||
{% block content %} | ||
|
||
{% if errors is defined and errors %} | ||
{% for error in errors%} | ||
<div class="alert alert-danger">{{ error|raw }}</div> | ||
{% endfor %} | ||
{% elseif success is defined and success %} | ||
<div class="alert alert-success">{{ success|raw }}</div> | ||
{% endif %} | ||
<form method="post" action="" role="form"> | ||
<div class="form-group"> | ||
<label for="name">Nome</label> | ||
<input type="text" class="form-control" id="name" name="name" required> | ||
</div> | ||
<div class="form-group"> | ||
<label for="email">E-mail</label> | ||
<input type="email" class="form-control" id="email" name="email" required> | ||
</div> | ||
<div class="form-group"> | ||
<label for="reason">Motivo</label> | ||
<select class="form-control" id="reason" name="reason"> | ||
<option>Atrasos</option> | ||
<option>Outros</option> | ||
</select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="message">Mensagem</label> | ||
<textarea class='form-control' name="message" required> | ||
|
||
</textarea> | ||
</div> | ||
<button type="submit" class="btn btn-default">Submit</button> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/LigaSolidariaStorage/Storage/Mailer/SenderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace LigaSolidariaStorage\Storage\Mailer; | ||
|
||
class SenderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
* @expectedException \LigaSolidariaStorage\Storage\Exception\InvalidMessageException | ||
*/ | ||
public function shouldNotSendEmailWithoutBody() | ||
{ | ||
$transportSender = $this->getMockBuilder('\Swift_Sender') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$sender = new Sender($transportSender); | ||
$message = new Message(); | ||
$message->setSubject('PHPSP rocks!'); | ||
$message->setFrom(array('[email protected]' => 'foo bar')); | ||
$message->setTo(array('[email protected]')); | ||
// without body: $message->setBody(''); | ||
|
||
$sender->send($message); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \LigaSolidariaStorage\Storage\Exception\InvalidMessageException | ||
*/ | ||
public function shouldNotSendEmailWithBodyOnlySpaces() | ||
{ | ||
$transportSender = $this->getMockBuilder('\Swift_Sender') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$sender = new Sender($transportSender); | ||
$message = new Message(); | ||
$message->setSubject('PHPSP rocks!'); | ||
$message->setFrom(array('[email protected]' => 'foo bar')); | ||
$message->setTo(array('[email protected]')); | ||
$message->setBody(' '); | ||
|
||
$sender->send($message); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Swift_SwiftException | ||
*/ | ||
public function shouldNotSendEmailWithoutFromEmail() | ||
{ | ||
$transportSender = $this->getMockBuilder('\Swift_Sender') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$sender = new Sender($transportSender); | ||
$message = new Message(); | ||
$message->setSubject('PHPSP rocks!'); | ||
$message->setFrom(array('foo' => 'foo bar')); | ||
$message->setTo(array('[email protected]')); | ||
$message->setBody(' '); | ||
|
||
$sender->send($message); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Swift_SwiftException | ||
*/ | ||
public function shouldNotSendEmailWithoutToEmail() | ||
{ | ||
$transportSender = $this->getMockBuilder('\Swift_Sender') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$sender = new Sender($transportSender); | ||
$message = new Message(); | ||
$message->setSubject('PHPSP rocks!'); | ||
$message->setFrom(array('[email protected]' => 'foo bar')); | ||
$message->setTo(array('foo')); | ||
$message->setBody(' '); | ||
|
||
$sender->send($message); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSendEmail() | ||
{ | ||
$transportSender = $this->getMockBuilder('\Swift_Sender') | ||
->disableOriginalConstructor() | ||
->setMethods(array('send')) | ||
->getMock(); | ||
|
||
$transportSender->expects($this->once()) | ||
->method('send') | ||
->will($this->returnValue(true)); | ||
|
||
$sender = new Sender($transportSender); | ||
$message = new Message(); | ||
$message->setSubject('PHPSP rocks!'); | ||
$message->setFrom(array('[email protected]' => 'foo bar')); | ||
$message->setTo(array('[email protected]')); | ||
$message->setBody('foobar'); | ||
|
||
$this->assertTrue($sender->send($message)); | ||
} | ||
|
||
} |