Skip to content

Commit

Permalink
improve handling missing credentials by fixing missed configuration v…
Browse files Browse the repository at this point in the history
…alidator checker

removed the configuration checking code from the parse function of the
provider class AwsS3Provider to the helper class CdnHelper
and update the validator to check for any required configuration if missed
and throw an exception

Issue: #10
  • Loading branch information
mahmoud committed Sep 5, 2014
1 parent 842049c commit e91cb53
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
31 changes: 31 additions & 0 deletions src/Vinelab/Cdn/CdnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use \Illuminate\Config\Repository;
use Vinelab\Cdn\Contracts\CdnHelperInterface;
use Vinelab\Cdn\Exceptions\MissingConfigurationException;
use Vinelab\Cdn\Exceptions\MissingConfigurationFileException;

/**
Expand Down Expand Up @@ -46,4 +47,34 @@ public function getConfigurations()
return $configurations;
}



/**
* Checks for any required configuration is missed
*
* @param $configuration
* @param $required
*
* @throws \Vinelab\Cdn\Exceptions\MissingConfigurationException
*/
public function validate($configuration, $required)
{
// search for any null or empty field to throw an exception
$missing = '';
foreach ($configuration as $key => $value) {

if (in_array($key, $required) &&
(empty($value) || $value == null || $value == ''))
{
$missing .= ' ' . $key;
}
}

if ($missing)
throw new MissingConfigurationException("Missed Configuration:" . $missing);

}



}
2 changes: 2 additions & 0 deletions src/Vinelab/Cdn/Contracts/CdnHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface CdnHelperInterface{

public function getConfigurations();

public function validate($configuration, $required);

}
38 changes: 21 additions & 17 deletions src/Vinelab/Cdn/Providers/AwsS3Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* @author Mahmoud Zalt <mahmoud@vinelab.com>
*/

use Vinelab\Cdn\Exceptions\MissingConfigurationException;
use Vinelab\Cdn\Providers\Contracts\ProviderInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Guzzle\Batch\BatchBuilder;
use Vinelab\Cdn\Contracts\CdnHelperInterface;

This comment has been minimized.

Copy link
@Mulkave

Mulkave Sep 5, 2014

Member

It's time for a configuration validator class instead of the helper doing this job.

use Aws\S3\Exception\S3Exception;
use Guzzle\Batch\BatchBuilder;
use Aws\S3\S3Client;

/**
Expand All @@ -35,6 +35,11 @@ class AwsS3Provider extends Provider implements ProviderInterface{
],
];

/**
* @var array
*/
protected $required_configurations = ['key', 'secret', 'buckets', 'domain'];

/**
* @var string
*/
Expand Down Expand Up @@ -91,12 +96,21 @@ class AwsS3Provider extends Provider implements ProviderInterface{
*/
protected $batch;

/**
* @var Vinelab\Cdn\Contracts\CdnHelperInterface
*/
protected $cdn_helper;

/**
* @param \Symfony\Component\Console\Output\ConsoleOutput $console
* @param \Vinelab\Cdn\Contracts\CdnHelperInterface $cdn_helper
*/
public function __construct(ConsoleOutput $console)
{
public function __construct(
ConsoleOutput $console,
CdnHelperInterface $cdn_helper
) {
$this->console = $console;
$this->cdn_helper = $cdn_helper;
}

/**
Expand Down Expand Up @@ -128,7 +142,6 @@ public function init($configurations)
*
* @param $configurations
*
* @throws MissingConfigurationException
* @return array
*/
private function parse($configurations)
Expand All @@ -137,18 +150,6 @@ private function parse($configurations)
// fill missed keys with null or default values.
$this->default = array_merge($this->default, $configurations);

// search for any null or empty field to throw an exception
$missing = '';
foreach ($this->default as $key => $value) {
// Fix: needs to check for the sub arrays also
if (empty($value) || $value == null || $value == '') {
$missing .= $key;
}
}

if ($missing)
throw new MissingConfigurationException("Missing Configurations:" . $missing);

// TODO: to be removed to a function of common configurations between call providers
$threshold = $this->default['threshold'];
$protocol = $this->default['protocol'];
Expand All @@ -171,6 +172,9 @@ private function parse($configurations)
'buckets' => $buckets,
];

// check if any required configuration is missed
$this->cdn_helper->validate($supplier, $this->required_configurations);

return $supplier;
}

Expand Down

0 comments on commit e91cb53

Please sign in to comment.