-
Notifications
You must be signed in to change notification settings - Fork 1
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
luiscoms
wants to merge
16
commits into
Dojo-Online:master
Choose a base branch
from
luiscoms:refactoring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactoring #7
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1d81016
Initializing refactoring
luiscoms 0841bfe
Fixed conflicts
luiscoms 5a7159e
Removed unused tests
luiscoms 8dbc19c
Make testAgeLessThan18ShouldBeInvalid pass
luiscoms f730837
Make testAgeGreaterThan60ShouldBeInvalid pass
luiscoms 7f2fe2c
Make testNameWithNumbersShouldBeInvalid pass
luiscoms e6a4e10
Make testNameSpecialCharsShouldBeInvalid pass
luiscoms 5ae3abe
Make testNameWithMoreThan15CharsShouldBeInvalid pass
luiscoms 45faddb
Make testDocumentWithLettersShouldBeInvalid pass
luiscoms d459710
Make testDocumentWithLessThan11NumbersShouldBeInvalid pass
luiscoms ea5a0eb
Make testDocumentWithMoreThan11NumbersShouldBeInvalid pass
luiscoms f6525bd
Make testDocumentWith11NumbersShouldBeValid pass
luiscoms 35114dc
Fixed test parameter
luiscoms a8a3a55
Added contract tests and adjustments on client structure
brunomrpx 955cf85
Finish 5, 6 and 7 requirements
luiscoms 64adbee
Removed return on constructor
luiscoms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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; | ||
} | ||
|
||
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); | ||
} | ||
} |
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,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
7
projeto/src/CarInsurance/Exception/InvalidClientException.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,7 @@ | ||
<?php | ||
|
||
namespace CarInsurance\Exception; | ||
|
||
class InvalidClientException extends \Exception | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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