Skip to content

Commit

Permalink
Drop Saver Factory
Browse files Browse the repository at this point in the history
Create savers in container to remove duplicate code
  • Loading branch information
glensc committed Sep 15, 2020
1 parent 71e7097 commit cfc9841
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 63 deletions.
52 changes: 0 additions & 52 deletions src/Xhgui/Saver.php

This file was deleted.

46 changes: 35 additions & 11 deletions src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use MongoDB\Driver\Manager;
use Pimple\Container;
use Slim\Slim;
use Slim\Views\Twig;
Expand Down Expand Up @@ -117,27 +118,50 @@ protected function _services()
};

$this['searcher'] = static function ($c) {
$config = $c['config'];
$saver = $c['config']['save.handler'];

return $c["searcher.$saver"];
};

switch ($config['save.handler']) {
case 'pdo':
return $c['searcher.pdo'];
$this['saver.mongo'] = static function ($c) {
$config = $c['config'];

case 'mongodb':
default:
return $c['searcher.mongo'];
if (!class_exists(Manager::class)) {
throw new RuntimeException("Required extension ext-mongodb missing");
}
$mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']);
$collection = $mongo->{$config['db.db']}->results;
$collection->findOne();

return new Xhgui_Saver_Mongo($collection);
};

$this['saver.mongo'] = static function ($c) {
$this['saver.pdo'] = static function ($c) {
$config = $c['config'];
$config['save.handler'] = 'mongodb';

return Xhgui_Saver::factory($config);
if (!class_exists(PDO::class)) {
throw new RuntimeException("Required extension ext-pdo is missing");
}

$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

return new Xhgui_Saver_Pdo(
new PDO(
$config['pdo']['dsn'],
$config['pdo']['user'],
$config['pdo']['pass'],
$options
),
$config['pdo']['table']
);
};

$this['saver'] = static function ($c) {
return Xhgui_Saver::factory($c['config']);
$saver = $c['config']['save.handler'];

return $c["saver.$saver"];
};
}

Expand Down

0 comments on commit cfc9841

Please sign in to comment.