diff --git a/readme.md b/readme.md index 7dcc1da..20a3625 100755 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ # CDN Assets Manager [![Build Status](https://travis-ci.org/thephpleague/statsd.png?branch=master)](https://travis-ci.org/Vinelab/cdn) - +[![Total Downloads](https://poser.pugx.org/league/statsd/downloads.png)](https://packagist.org/packages/vinelab/cdn) Content Delivery Network Package for Laravel 4 Upload static assets of your choice to a CDN and have the file paths replaced with full URLs. @@ -90,13 +90,13 @@ Supported Providers: Specify directories, extensions, files and patterns to be uploaded. - ```php - 'include' => [ - 'directories' => ['public/dist'], - 'extensions' => ['.js', '.css', '.yxz'], - 'patterns' => ['**/*.coffee'], - ], - ``` +```php + 'include' => [ + 'directories' => ['public/dist'], + 'extensions' => ['.js', '.css', '.yxz'], + 'patterns' => ['**/*.coffee'], + ], +``` #### Exclude diff --git a/src/Vinelab/Cdn/CdnHelper.php b/src/Vinelab/Cdn/CdnHelper.php index 990d01d..788b88a 100644 --- a/src/Vinelab/Cdn/CdnHelper.php +++ b/src/Vinelab/Cdn/CdnHelper.php @@ -6,6 +6,7 @@ use \Illuminate\Config\Repository; use Vinelab\Cdn\Contracts\CdnHelperInterface; +use Vinelab\Cdn\Exceptions\MissingConfigurationException; use Vinelab\Cdn\Exceptions\MissingConfigurationFileException; /** @@ -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); + + } + + + } diff --git a/src/Vinelab/Cdn/CdnServiceProvider.php b/src/Vinelab/Cdn/CdnServiceProvider.php index e99efd8..c2b1d36 100755 --- a/src/Vinelab/Cdn/CdnServiceProvider.php +++ b/src/Vinelab/Cdn/CdnServiceProvider.php @@ -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: //----------------------- diff --git a/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php b/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php index 9e089ab..2b9a2cb 100644 --- a/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php +++ b/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php @@ -8,4 +8,6 @@ interface CdnHelperInterface{ public function getConfigurations(); + public function validate($configuration, $required); + } diff --git a/src/Vinelab/Cdn/Providers/AwsS3Provider.php b/src/Vinelab/Cdn/Providers/AwsS3Provider.php index b217c14..cf065b7 100755 --- a/src/Vinelab/Cdn/Providers/AwsS3Provider.php +++ b/src/Vinelab/Cdn/Providers/AwsS3Provider.php @@ -4,19 +4,28 @@ * @author Mahmoud Zalt */ -use Vinelab\Cdn\Exceptions\MissingConfigurationException; +use Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface; use Vinelab\Cdn\Providers\Contracts\ProviderInterface; use Symfony\Component\Console\Output\ConsoleOutput; -use Guzzle\Batch\BatchBuilder; +use Vinelab\Cdn\Contracts\CdnHelperInterface; use Aws\S3\Exception\S3Exception; +use Guzzle\Batch\BatchBuilder; 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, @@ -35,6 +44,13 @@ class AwsS3Provider extends Provider implements ProviderInterface{ ], ]; + /** + * Required configurations (must exist in the config file) + * + * @var array + */ + protected $rules = ['key', 'secret', 'buckets', 'domain']; + /** * @var string */ @@ -91,12 +107,29 @@ class AwsS3Provider extends Provider implements ProviderInterface{ */ protected $batch; + /** + * @var Vinelab\Cdn\Contracts\CdnHelperInterface + */ + 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) - { - $this->console = $console; + public function __construct( + ConsoleOutput $console, + ConfigurationsInterface $configurations, + CdnHelperInterface $cdn_helper + ) { + $this->console = $console; + $this->configurations = $configurations; + $this->cdn_helper = $cdn_helper; } /** @@ -128,7 +161,6 @@ public function init($configurations) * * @param $configurations * - * @throws MissingConfigurationException * @return array */ private function parse($configurations) @@ -137,18 +169,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']; @@ -171,6 +191,9 @@ private function parse($configurations) 'buckets' => $buckets, ]; + // check if any required configuration is missed + $this->configurations->validate($supplier, $this->rules); + return $supplier; } diff --git a/src/Vinelab/Cdn/Validators/Configurations.php b/src/Vinelab/Cdn/Validators/Configurations.php new file mode 100644 index 0000000..6ee682f --- /dev/null +++ b/src/Vinelab/Cdn/Validators/Configurations.php @@ -0,0 +1,42 @@ + + */ + +use Vinelab\Cdn\Exceptions\MissingConfigurationException; +use Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface; + +/** + * Class Configurations + * @package Vinelab\Cdn\Validators + */ +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); + + } + +} diff --git a/src/Vinelab/Cdn/Validators/Contracts/ConfigurationsInterface.php b/src/Vinelab/Cdn/Validators/Contracts/ConfigurationsInterface.php new file mode 100755 index 0000000..ca512db --- /dev/null +++ b/src/Vinelab/Cdn/Validators/Contracts/ConfigurationsInterface.php @@ -0,0 +1,9 @@ + + */ + +interface ConfigurationsInterface{ + +}