Skip to content

Commit

Permalink
Merge pull request #12 from Vinelab/fix/upload-with-missing-credentials
Browse files Browse the repository at this point in the history
improve handling missing credentials by fixing missed configuration vali...
  • Loading branch information
Mulkave committed Sep 5, 2014
2 parents 3180580 + 1321a39 commit 9575d26
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 27 deletions.
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
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);

}



}
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
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);

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

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,
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -128,7 +161,6 @@ public function init($configurations)
*
* @param $configurations
*
* @throws MissingConfigurationException
* @return array
*/
private function parse($configurations)
Expand All @@ -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'];
Expand All @@ -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;
}

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

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

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

}

}
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 9575d26

Please sign in to comment.