Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Galek | ||
* Date: 6.4.2018 | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Galek\Utils\Calendar\DI; | ||
|
||
|
||
use Galek\Utils\Calendar\Configuration\Configurator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
|
||
class SymfonyExtension extends \Symfony\Component\DependencyInjection\Extension\Extension | ||
{ | ||
use TExtension; | ||
|
||
/** | ||
* Loads a specific configuration. | ||
* | ||
* @throws \InvalidArgumentException When provided tag is not defined in this extension | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$this->checkConfig($configs); | ||
|
||
$container->register('galek.calendar', Configurator::class) | ||
->addArgument($configs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Galek | ||
* Date: 6.4.2018 | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Galek\Utils\Calendar\DI; | ||
|
||
|
||
trait TExtension | ||
{ | ||
protected $defaultWork = [ | ||
'start' => [ | ||
'hour' => 8, | ||
'minute' => 0, | ||
], | ||
'end' => [ | ||
'hour' => 16, | ||
'minute' => 30, | ||
], | ||
'weekend' => false, | ||
]; | ||
|
||
protected $defaultShipper = [ | ||
'endHour' => 14, | ||
'endMinute' => 0, | ||
'weekend' => false, | ||
'deliveryTime' => 1, | ||
]; | ||
|
||
|
||
public function loadConfiguration(): void | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$config = $this->getConfig(); | ||
|
||
$this->checkConfig($config); | ||
|
||
$builder->addDefinition($this->prefix('galek.calendar')) | ||
->setFactory(Configurator::class, [$config]); | ||
} | ||
|
||
|
||
protected function checkConfig(array & $config): void | ||
{ | ||
$this->checkCountry($config); | ||
$this->checkShippers($config); | ||
$this->checkWork($config); | ||
} | ||
|
||
|
||
protected function checkCountry(array & $config): void | ||
{ | ||
foreach ($config as $group => $setting) { | ||
if (!array_key_exists('country', $setting)) { | ||
$config[$group][ 'country' ] = 'CzechRepublic'; | ||
} | ||
} | ||
} | ||
|
||
|
||
protected function checkShippers(array & $config): void | ||
{ | ||
foreach ($config as $group => $setting) { | ||
if (!array_key_exists('shippers', $setting)) { | ||
$config[$group]['shippers'] = []; | ||
} else { | ||
foreach ($setting['shippers'] as $name => $shipper) { | ||
if (!array_key_exists('endHour', $shipper)) { | ||
$config[$group]['shippers'][$name]['endHour'] = $this->defaultShipper['endHour']; | ||
} | ||
if (!array_key_exists('endMinute', $shipper)) { | ||
$config[$group]['shippers'][$name]['endMinute'] = $this->defaultShipper['endMinute']; | ||
} | ||
if (!array_key_exists('weekend', $shipper)) { | ||
$config[$group]['shippers'][$name]['weekend'] = $this->defaultShipper['weekend']; | ||
} | ||
if (!array_key_exists('deliveryTime', $shipper)) { | ||
$config[$group]['shippers'][$name]['deliveryTime'] = $this->defaultShipper['deliveryTime']; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
protected function checkWork(array & $config): void | ||
{ | ||
foreach ($config as $group => $setting) { | ||
if (!array_key_exists('work', $setting)) { | ||
$config[$group]['work'] = $this->defaultWork; | ||
} else { | ||
if (!array_key_exists('start', $setting['work'])) { | ||
$config[$group]['work']['start'] = $this->defaultWork['start']; | ||
} else { | ||
if (!array_key_exists('hour', $setting['work']['start'])) { | ||
$config[$group]['work']['start']['hour'] = $this->defaultWork['start']['hour']; | ||
} | ||
if (!array_key_exists('minute', $setting['work']['start'])) { | ||
$config[$group]['work']['start']['minute'] = $this->defaultWork['start']['minute']; | ||
} | ||
} | ||
if (!array_key_exists('end', $setting['work'])) { | ||
$config[$group]['work']['end'] = $this->defaultWork['end']; | ||
} else { | ||
if (!array_key_exists('hour', $setting['work']['end'])) { | ||
$config[$group]['work']['end']['hour'] = $this->defaultWork['end']['hour']; | ||
} | ||
if (!array_key_exists('minute', $setting['work']['start'])) { | ||
$config[$group]['work']['end']['minute'] = $this->defaultWork['end']['minute']; | ||
} | ||
} | ||
if (!array_key_exists('weekend', $setting['work'])) { | ||
$config[$group]['work']['weekend'] = $this->defaultWork['weekend']; | ||
} | ||
} | ||
} | ||
} | ||
} |