This commit is contained in:
R. Eric Wheeler 2016-07-11 14:58:53 -07:00
parent 15627da05c
commit a00d9b49a0
1 changed files with 45 additions and 47 deletions

View File

@ -16,82 +16,80 @@ use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
use Silex\Api\BootableProviderInterface; use Silex\Api\BootableProviderInterface;
use Silex\Application; use Silex\Application;
use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\{
use Symfony\Component\Validator\Constraints\Email; Collection,
use Symfony\Component\Validator\Constraints\Length; Email,
use Symfony\Component\Validator\Constraints\NotBlank; Length,
use Symfony\Component\Validator\Constraints\NotNull; NotBlank,
use Symfony\Component\Validator\Constraints\Regex; NotNull,
Regex
};
/** /**
* Class ConfigServiceProvider * Class ConfigServiceProvider
* *
* @package Sikofitt\Config * @package Sikofitt\Config
*/ */
class ConfigServiceProvider implements ServiceProviderInterface, BootableProviderInterface class ConfigServiceProvider implements ServiceProviderInterface, BootableProviderInterface {
{
/** /**
* @param Container $app * @param Container $app
*/ */
public function register(Container $app) public function register(Container $app) {
{ $app['config'] = function ($app) {
$app['config'] = function ($app) {
$config = Config::load($app['config.path']); $config = Config::load($app['config.path']);
return $config; return $config;
}; };
} }
public function boot(Application $app) public function boot(Application $app) {
{ $configItems = [
$configItems = [
'email' => $app->config('app.email'), 'email' => $app->config('app.email'),
'phone' => $app->config('app.phone'), 'phone' => $app->config('app.phone'),
]; ];
$constraints = [
'email' => [
new NotNull(['message' => 'Email value in app config is not present.']),
new NotBlank(['message' => 'Email should cannot be blank in config.']),
new Email(['message' => sprintf('Invalid email address in config. (%s)', $configItems['email'])]),
],
'phone' => [
new NotNull(['message' => 'Phone number value in app config is not present.']),
new NotBlank(['message' => 'Phone number cannot be blank in config.']),
new Length([
'min' => 10,
'minMessage' => sprintf('Invalid phone number, it should have at least 10 characters. %s given.', count($configItems['phone'])),
]),
],
];
$constraints = [ $captcha = $app->config('app.captcha');
'email' => [ if (isset($captcha) && $captcha) {
new NotNull(['message' => 'Email value in app config is not present.']), $configItems['captcha_sitekey'] = $app->config('app.captcha_sitekey');
new NotBlank(['message' => 'Email should cannot be blank in config.']), $configItems['captcha_secret'] = $app->config('app.captcha_secret');
new Email(['message' => sprintf('Invalid email address in config. (%s)', $configItems['email'])]),
],
'phone' => [
new NotNull(['message' => 'Phone number value in app config is not present.']),
new NotBlank(['message' => 'Phone number cannot be blank in config.']),
new Length([
'min' => 10,
'minMessage' => sprintf('Invalid phone number, it should have at least 10 characters. %s given.', count($configItems['phone'])),
]),
],
];
$captcha = $app->config('app.captcha'); $constraints['captcha_sitekey'] = [
if (isset($captcha) && $captcha) {
$configItems['captcha_sitekey'] = $app->config('app.captcha_sitekey');
$configItems['captcha_secret'] = $app->config('app.captcha_secret');
$constraints['captcha_sitekey'] = [
new NotNull(['message' => 'ReCaptcha sitekey is a required value to use the captcha, this check can be disabled by removing or setting the captcha config item to false.']), new NotNull(['message' => 'ReCaptcha sitekey is a required value to use the captcha, this check can be disabled by removing or setting the captcha config item to false.']),
new NotBlank(), new NotBlank(),
new Length(['min' => 40]), new Length(['min' => 40]),
]; ];
$constraints['captcha_secret'] = [ $constraints['captcha_secret'] = [
new NotNull(), new NotNull(),
new NotBlank(), new NotBlank(),
new Length(['min' => 40]), new Length(['min' => 40]),
]; ];
}
$errors = $app['validator']->validate($configItems, new Collection($constraints));
$validationErrors = [];
foreach ($errors->getIterator() as $error) {
$validationErrors[] = $error->getMessage();
}
if (count($validationErrors) > 0) {
throw new MissingConfigurationItemException($validationErrors[0]);
}
} }
$errors = $app['validator']->validate($configItems, new Collection($constraints));
$validationErrors = [];
foreach ($errors->getIterator() as $error) {
$validationErrors[] = $error->getMessage();
}
if (count($validationErrors) > 0) {
throw new MissingConfigurationItemException($validationErrors[0]);
}
}
} }