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 Silex\Api\BootableProviderInterface;
use Silex\Application;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\{
Collection,
Email,
Length,
NotBlank,
NotNull,
Regex
};
/**
* Class ConfigServiceProvider
*
* @package Sikofitt\Config
*/
class ConfigServiceProvider implements ServiceProviderInterface, BootableProviderInterface
{
class ConfigServiceProvider implements ServiceProviderInterface, BootableProviderInterface {
/**
/**
* @param Container $app
*/
public function register(Container $app)
{
$app['config'] = function ($app) {
public function register(Container $app) {
$app['config'] = function ($app) {
$config = Config::load($app['config.path']);
return $config;
};
}
public function boot(Application $app)
{
$configItems = [
public function boot(Application $app) {
$configItems = [
'email' => $app->config('app.email'),
'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 = [
'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'])),
]),
],
];
$captcha = $app->config('app.captcha');
if (isset($captcha) && $captcha) {
$configItems['captcha_sitekey'] = $app->config('app.captcha_sitekey');
$configItems['captcha_secret'] = $app->config('app.captcha_secret');
$captcha = $app->config('app.captcha');
if (isset($captcha) && $captcha) {
$configItems['captcha_sitekey'] = $app->config('app.captcha_sitekey');
$configItems['captcha_secret'] = $app->config('app.captcha_secret');
$constraints['captcha_sitekey'] = [
$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 NotBlank(),
new Length(['min' => 40]),
];
$constraints['captcha_secret'] = [
$constraints['captcha_secret'] = [
new NotNull(),
new NotBlank(),
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]);
}
}
}