Skip to content

Commit

Permalink
validate limits before generating password
Browse files Browse the repository at this point in the history
  • Loading branch information
hackzilla committed Jul 23, 2016
1 parent e0a8cdf commit 0e912ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Exception/ImpossibleMinMaxLimitsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Hackzilla\PasswordGenerator\Exception;

class ImpossibleMinMaxLimitsException extends \Exception
{
}
26 changes: 16 additions & 10 deletions Generator/RequirementPasswordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hackzilla\PasswordGenerator\Generator;

use Hackzilla\PasswordGenerator\Exception\ImpossibleMinMaxLimitsException;
use Hackzilla\PasswordGenerator\Exception\InvalidOptionException;

/**
Expand All @@ -16,6 +17,7 @@ class RequirementPasswordGenerator extends ComputerPasswordGenerator
private $minimumCounts = array();
private $maximumCounts = array();
private $validOptions = array();
private $dirtyCheck = true;

/**
*/
Expand All @@ -35,22 +37,22 @@ public function __construct()
* Generate one password based on options.
*
* @return string password
* @throws ImpossibleMinMaxLimitsException
* @throws \Hackzilla\PasswordGenerator\Exception\CharactersNotFoundException
*/
public function generatePassword()
{
$characterList = $this->getCharacterList()->getCharacters();
$characters = \strlen($characterList);
$password = '';

$length = $this->getLength();
if ($this->dirtyCheck) {
if (!$this->validLimits()) {
throw new ImpossibleMinMaxLimitsException();
}

for ($i = 0; $i < $length; ++$i) {
$password .= $characterList[$this->randomInteger(0, $characters - 1)];
$this->dirtyCheck = false;
}

if (!$this->validatePassword($password)) {
$password = $this->generatePassword();
}
do {
$password = parent::generatePassword();
} while (!$this->validatePassword($password));

return $password;
}
Expand Down Expand Up @@ -91,6 +93,8 @@ public function getMaximumCount($option)
*/
public function setMinimumCount($option, $characterCount)
{
$this->dirtyCheck = true;

if (!$this->validOption($option)) {
throw new InvalidOptionException('Invalid Option');
}
Expand Down Expand Up @@ -122,6 +126,8 @@ public function setMinimumCount($option, $characterCount)
*/
public function setMaximumCount($option, $characterCount)
{
$this->dirtyCheck = true;

if (!$this->validOption($option)) {
throw new InvalidOptionException('Invalid Option');
}
Expand Down

0 comments on commit 0e912ce

Please sign in to comment.