Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #7

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Para criar a imagem é preciso rodar o comando a partir da raiz:

Ocorrendo sucesso, com uma saída parecida com `Successfully built 66683d59bc88`, podemos rodar os testes com o seguinte comando:

sudo docker run -it --rm dojo-online/carinsurance phpunit
sudo docker run -it --rm -v ${PWD}:/carinsurance dojo-online/carinsurance phpunit

Ou ainda entrar no shell da imagem com o comando

Expand Down
133 changes: 133 additions & 0 deletions projeto/src/CarInsurance/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace CarInsurance;

use CarInsurance\Exception\InvalidClientException;

class Client
{
private $name;
private $document;
private $birthdate;
private $phoneNumber;
private $homeNumber;
private $email;
private $maritalStatus;
private $gender;
private $cnhPoints = 0;

const MALE = 'M';
const FEMALE = 'F';

const SINGLE = 'S';
const MARRIED = 'M';

public function __construct($name, $document, \DateTime $birthdate, $gender)
{
$this
->setName($name)
->setDocument($document)
->setBirthdate($birthdate)
->setGender($gender);

return $this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Construtor não tem retorno

}

public function setGender($gender)
{
$this->gender = $gender;
return $this;
}

public function getGender()
{
return $this->gender;
}

public function setName($name)
{
$name = trim($name);
if (!$this->isValidName($name)) {
throw new InvalidClientException("Client name length should be less than 15 without numbers or special chars");
}
$this->name = $name;
return $this;
}

public function setDocument($document)
{
if (!$this->isValidDocument($document)) {
throw new InvalidClientException("Client document should be a numeric with 11 characters");
}
$this->document = $document;
return $this;
}

public function setBirthdate(\DateTime $birthdate)
{
if (!$this->isValidAge($birthdate)) {
throw new InvalidClientException("Client age should be between 18 and 60");
}
$this->birthdate = $birthdate;
return $this;
}

public function getAge()
{
return $this->birthdate->diff(new \DateTime)->format('%Y');
}

public function setHomeNumber($homeNumber)
{
return $this;
}

public function setPhoneNumber($phoneNumber)
{
return $this;
}

public function setEmail($email)
{
return $this;
}

public function setMaritalStatus($maritalStatus)
{
$this->maritalStatus = $maritalStatus;
return $this;
}

public function getMaritalStatus()
{
return $this->maritalStatus;
}

public function setCNHPoints($cnhPoints)
{
$this->cnhPoints = $cnhPoints;
return $this;
}

public function getCNHPoints()
{
return $this->cnhPoints;
}

private function isValidAge(\DateTime $birthdate)
{
$now = new \DateTime();
$interval = $now->diff($birthdate);
return $interval->y >= 18 && $interval->y <= 60;
}

private function isValidName($name)
{
return (bool)preg_match("/^[a-z]{2}[a-z ]{0,13}$/i", $name);
}

private function isValidDocument($document)
{
return is_numeric($document) && (strlen($document) == 11);
}
}
54 changes: 54 additions & 0 deletions projeto/src/CarInsurance/Contract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace CarInsurance;

use CarInsurance\Client;

class Contract
{
private $number;
private $client;

public function __construct($number, Client $client)
{
$this->setNumber($number);
$this->setClient($client);
}

private function setNumber($number)
{
$this->number = $number;
return $this;
}

private function setClient(Client $client)
{
$this->client = $client;
return $this;
}

public function getClient()
{
return $this->client;
}

public function getDiscount()
{
$client = $this->getClient();
if ($client->getGender() == $client::FEMALE) {
$age = $client->getAge();
if ($age <= 23 && !$client->getCNHPoints()) {
return 50;
}

if ($age > 30 && $client->getMaritalStatus() == Client::SINGLE && $client->getCNHPoints() == 0) {
return 10;
}

if ($age > 23 && $client->getMaritalStatus() == Client::MARRIED) {
return 20;
}
}
return 0;
}
}
7 changes: 7 additions & 0 deletions projeto/src/CarInsurance/Exception/InvalidClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace CarInsurance\Exception;

class InvalidClientException extends \Exception
{
}
70 changes: 0 additions & 70 deletions projeto/src/SeguradoraCarro/Cliente.php

This file was deleted.

77 changes: 0 additions & 77 deletions projeto/src/SeguradoraCarro/RegrasDeNegocio.php

This file was deleted.

Loading