Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Settings #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/DI/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
WooSms\Event\Loader\OrderStatus,
WooSms\Event\Loader\Post,
WooSms\Event\Loader\Product,
WooSms\Event\Loader\Shop};
WooSms\Event\Loader\Shop,
WooSms\Event\OrderForm};
use function extension_loaded, is_int, class_exists, in_array;

class Factory implements DIFactory
Expand Down Expand Up @@ -122,11 +123,6 @@ protected static function createContainer(array $parameters = []): Container
])];
$container['event.dispatcher'] = Event\Dispatcher::class;

if (in_array($parameters['dispatcher'] ?? null, [Event\Dispatcher::Asset, Event\Dispatcher::Cron, Event\Dispatcher::Direct], true))
{
Event\Dispatcher::$default_dispatcher = $parameters['dispatcher'];
}

// IO
$container['io.connection.factory'] = ['factory' => IO\ConnectionFactory::class, 'factory_method' => function () use ($container): IO\ConnectionFactory
{
Expand All @@ -147,7 +143,24 @@ protected static function createContainer(array $parameters = []): Container

// Settings
$container['settings.repository.database'] = Settings\Repository\SettingsDatabase::class;
$container['settings.settings'] = Settings\Settings::class;
$container['settings.settings'] = ['factory' => Settings\Settings::class, 'factory_method' => function () use ($container, $parameters): Settings\Settings
{
$service = new Settings\Settings($container->getByClass( Settings\Repository\Settings::class));
$service->setDefaultSettings([
'main:dispatcher' => in_array($parameters['dispatcher'] ?? null, [Event\Dispatcher::Asset, Event\Dispatcher::Cron, Event\Dispatcher::Direct], true) ? $parameters['dispatcher'] : Event\Dispatcher::Direct,
'main:synchronization' => 'all',
'main:language' => 'auto',
'main:language_mutation' => false,
'main:delete_db' => false,
'main:address_preference' => 'delivery',
'main:marketing_message_opt_in_enabled' => OrderForm::DefaultEnabled,
'main:marketing_message_opt_in_label' => '',
'main:marketing_message_opt_in_default' => false,
'main:marketing_message_opt_in_url' => '',
'main:marketing_message_opt_in_url_label' => '',
]);
return $service;
}];
$container['settings.repository.synchronizer'] = Settings\Repository\SynchronizationDatabase::class;
$container['settings.synchronizer'] = Settings\Synchronizer::class;

Expand Down
2 changes: 1 addition & 1 deletion src/Eshop/LanguageWordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function load(): array

public function get(?int $id = null): string
{
if ($this->hasMultiLanguageSupport() && defined('ICL_LANGUAGE_CODE'))
if ($this->hasMultiLanguageSupport() && defined('ICL_LANGUAGE_CODE'))
{
return $id === null ? ICL_LANGUAGE_CODE : ((string) get_post_meta($id, 'wpml_language', true) ?: ICL_LANGUAGE_CODE);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Template/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public static function print(Container $di): void
];

$plugin_settings = [
'dispatcher' => $settings->load('main:dispatcher') ?? Dispatcher::$default_dispatcher,
'synchronization' => $settings->load('main:synchronization') ?? 'all',
'language' => $settings->load('main:language') ?? 'en',
'language_mutation' => $settings->load('main:language_mutation') ?? false,
'delete_db' => $settings->load('main:delete_db') ?? false,
'address_preference' => $settings->load('main:address_preference') ?? 'delivery',
'marketing_message_opt_in_enabled' => $settings->load('main:marketing_message_opt_in_enabled') ?? OrderForm::DefaultEnabled,
'marketing_message_opt_in_label' => $settings->load('main:marketing_message_opt_in_label') ?? '',
'marketing_message_opt_in_default' => $settings->load('main:marketing_message_opt_in_default') ?? false,
'marketing_message_opt_in_url' => $settings->load('main:marketing_message_opt_in_url') ?? '',
'dispatcher' => $settings->load('main:dispatcher'),
'synchronization' => $settings->load('main:synchronization'),
'language' => $settings->load('main:language'),
'language_mutation' => $settings->load('main:language_mutation'),
'delete_db' => $settings->load('main:delete_db'),
'address_preference' => $settings->load('main:address_preference'),
'marketing_message_opt_in_enabled' => $settings->load('main:marketing_message_opt_in_enabled'),
'marketing_message_opt_in_label' => $settings->load('main:marketing_message_opt_in_label'),
'marketing_message_opt_in_default' => $settings->load('main:marketing_message_opt_in_default'),
'marketing_message_opt_in_url' => $settings->load('main:marketing_message_opt_in_url'),
];

wp_print_inline_script_tag(<<<JS
Expand Down
4 changes: 0 additions & 4 deletions tests/DI/FactoryTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class FactoryTest extends TestCase

public function testBase(): void
{
Assert::same(Dispatcher::Direct, Dispatcher::$default_dispatcher);

Factory::setup(fn () => [
'db' => Mockery::mock(wpdb::class),
'url' => 'https://www.bulkgate.com',
Expand Down Expand Up @@ -127,8 +125,6 @@ class FactoryTest extends TestCase
Assert::type(Sign::class, Factory::get()->getService('user.sign'));

Assert::count(35, Factory::get());

Assert::same(Dispatcher::Cron, Dispatcher::$default_dispatcher);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Template/BasicTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BasicTest extends TestCase
$settings->shouldReceive('load')->with('main:marketing_message_opt_in_enabled')->once()->andReturnFalse();
$settings->shouldReceive('load')->with('main:marketing_message_opt_in_label')->once()->andReturn('label');
$settings->shouldReceive('load')->with('main:marketing_message_opt_in_default')->once()->andReturnFalse();
$settings->shouldReceive('load')->with('main:marketing_message_opt_in_url')->once()->andReturnNull();
$settings->shouldReceive('load')->with('main:marketing_message_opt_in_url')->once()->andReturn('');
$url->shouldReceive('get')->withNoArgs()->once()->andReturn('https://www.example.com/');
$url->shouldReceive('get')->with('widget/eshop/load/jwt.token.451?config=init_widget_eshop_load')->once()->andReturn('https://www.example.com/widget/message/send/jwt.token.451?config=init_widget_message_send');
$url->shouldReceive('get')->with('/images/white-label/bulkgate/logo/short.svg')->once()->andReturn('https://www.example.com/images/white-label/bulkgate/logo/short.svg');
Expand Down