Skip to content

Commit

Permalink
add configuration validator class
Browse files Browse the repository at this point in the history
- remove configuration validate from helper class and create a configuration validator class
to validate the configurations
- rename $required_configurations to $rules in the amazon provider class

Fix #12
  • Loading branch information
mahmoud committed Sep 5, 2014
1 parent e91cb53 commit fea73ac
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Vinelab/Cdn/CdnServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function register()
'Vinelab\Cdn\CdnHelper'
);


$this->app->bind(
'Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface',
'Vinelab\Cdn\Validators\Configurations'
);

// register the commands:
//-----------------------
Expand Down
31 changes: 25 additions & 6 deletions src/Vinelab/Cdn/Providers/AwsS3Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author Mahmoud Zalt <mahmoud@vinelab.com>
*/

use Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface;
use Vinelab\Cdn\Providers\Contracts\ProviderInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Vinelab\Cdn\Contracts\CdnHelperInterface;
Expand All @@ -12,11 +13,19 @@
use Aws\S3\S3Client;

/**
* Amazon (AWS) S3
*
* Class AwsS3Provider
* @package Vinelab\Cdn\Provider
*/
class AwsS3Provider extends Provider implements ProviderInterface{

/**
* All the configurations needed by this class with the
* optional configurations default values
*
* @var array
*/
protected $default = [
'protocol' => 'https',
'domain' => null,
Expand All @@ -36,9 +45,11 @@ class AwsS3Provider extends Provider implements ProviderInterface{
];

/**
* Required configurations (must exist in the config file)
*
* @var array
*/
protected $required_configurations = ['key', 'secret', 'buckets', 'domain'];
protected $rules = ['key', 'secret', 'buckets', 'domain'];

/**
* @var string
Expand Down Expand Up @@ -101,16 +112,24 @@ class AwsS3Provider extends Provider implements ProviderInterface{
*/
protected $cdn_helper;

/**
* @var \Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface
*/
protected $configurations;

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

/**
Expand Down Expand Up @@ -173,7 +192,7 @@ private function parse($configurations)
];

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

return $supplier;
}
Expand Down
44 changes: 44 additions & 0 deletions src/Vinelab/Cdn/Validators/Configurations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php namespace Vinelab\Cdn\Validators;

/**
* @author Mahmoud Zalt <mahmoud@vinelab.com>
*/

use Vinelab\Cdn\Exceptions\MissingConfigurationException;
use Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface;

/**
* Helper class containing shared functions
*
* Class CdnHelper

This comment has been minimized.

Copy link
@Mulkave

Mulkave Sep 5, 2014

Member

not the correct class description

* @package Vinelab\Cdn
*/
class Configurations implements ConfigurationsInterface{

/**
* 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);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace Vinelab\Cdn\Validators\Contracts;

/**
* @author Mahmoud Zalt <mahmoud@vinelab.com>
*/

interface ConfigurationsInterface{

}

0 comments on commit fea73ac

Please sign in to comment.