Skip to content

Commit

Permalink
Use the factories instead of messing about with the container (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
punkstar authored Apr 23, 2018
1 parent b75d23e commit 4c9c7f9
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 78 deletions.
17 changes: 1 addition & 16 deletions etc/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,7 @@
$commands = [];

foreach ($c->get('commands') as $command) {
$manualArgs = [];

switch ($command) {
case 'Meanbee\Magedbm2\Command\LsCommand':
// Ensure that we get two different instances of the object for our LsCommand. The default behaviour
// of the container would give us the same instance.
$manualArgs['dataStorage'] = $c->make(\Meanbee\Magedbm2\Service\StorageInterface::class);
case 'Meanbee\Magedbm2\Command\ExportCommand':
case 'Meanbee\Magedbm2\Command\GetCommand':
case 'Meanbee\Magedbm2\Command\ImportCommand':
case 'Meanbee\Magedbm2\Command\PutCommand':
case 'Meanbee\Magedbm2\Command\RmCommand':
$manualArgs['storage'] = $c->make(\Meanbee\Magedbm2\Service\StorageInterface::class);
}

$commands[] = $c->make($command, $manualArgs);
$commands[] = $c->make($command);
}

foreach ($commands as $command) {
Expand Down
8 changes: 5 additions & 3 deletions src/Command/ConfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Meanbee\Magedbm2\Application\ConfigFileResolver;
use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Service\FilesystemFactory;
use Meanbee\Magedbm2\Service\FilesystemInterface;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -44,21 +45,22 @@ class ConfigureCommand extends BaseCommand

/**
* @param ConfigInterface $config
* @param FilesystemInterface $filesystem
* @param ConfigFileResolver $configFileResolver
* @param FilesystemFactory $filesystemFactory
* @param Yaml $yaml
* @param array|null $excluded_options
*/
public function __construct(
ConfigInterface $config,
ConfigFileResolver $configFileResolver,
FilesystemInterface $filesystem,
FilesystemFactory $filesystemFactory,
Yaml $yaml,
array $excluded_options = null
) {
parent::__construct($config, self::NAME);

$this->config = $config;
$this->filesystem = $filesystem;
$this->filesystem = $filesystemFactory->create();
$this->yaml = $yaml;

if ($excluded_options) {
Expand Down
14 changes: 8 additions & 6 deletions src/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Meanbee\Magedbm2\Application\Config\Option;
use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Service\Anonymiser\Export;
use Meanbee\Magedbm2\Service\FilesystemFactory;
use Meanbee\Magedbm2\Service\FilesystemInterface;
use Meanbee\Magedbm2\Service\StorageFactory;
use Meanbee\Magedbm2\Service\StorageInterface;
use Meanbee\Magedbm2\Shell\Command\Gzip;
use Meanbee\Magedbm2\Shell\Command\Mysqldump;
Expand Down Expand Up @@ -34,17 +36,17 @@ class ExportCommand extends BaseCommand

/**
* @param ConfigInterface $config
* @param StorageInterface $storage
* @param FilesystemInterface $filesystem
* @param StorageFactory $storageFactory
* @param FilesystemFactory $filesystemFactory
*/
public function __construct(ConfigInterface $config, StorageInterface $storage, FilesystemInterface $filesystem)
public function __construct(ConfigInterface $config, StorageFactory $storageFactory, FilesystemFactory $filesystemFactory)
{
parent::__construct($config, self::NAME);
$this->anonymiser = new Export();
$this->storage = $storage;
$this->filesystem = $filesystem;
$this->storage = $storageFactory->create();
$this->filesystem = $filesystemFactory->create();

$storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA);
$this->storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA);

$this->ensureServiceConfigurationValidated('storage', $this->storage);
}
Expand Down
25 changes: 13 additions & 12 deletions src/Command/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Application\Config\Option;
use Meanbee\Magedbm2\Service\DatabaseFactory;
use Meanbee\Magedbm2\Service\DatabaseInterface;
use Meanbee\Magedbm2\Service\FilesystemFactory;
use Meanbee\Magedbm2\Service\FilesystemInterface;
use Meanbee\Magedbm2\Exception\ServiceException;
use Meanbee\Magedbm2\Service\StorageFactory;
use Meanbee\Magedbm2\Service\StorageInterface;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -32,26 +35,24 @@ class GetCommand extends BaseCommand
protected $filesystem;

/**
* GetCommand constructor.
*
* @param ConfigInterface $config
* @param DatabaseInterface $database
* @param StorageInterface $storage
* @param FilesystemInterface $filesystem
* @param DatabaseFactory $databaseFactory
* @param StorageFactory $storageFactory
* @param FilesystemFactory $filesystemFactory
*/
public function __construct(
ConfigInterface $config,
DatabaseInterface $database,
StorageInterface $storage,
FilesystemInterface $filesystem
DatabaseFactory $databaseFactory,
StorageFactory $storageFactory,
FilesystemFactory $filesystemFactory
) {
parent::__construct($config, self::NAME);

$this->database = $database;
$this->storage = $storage;
$this->filesystem = $filesystem;
$this->database = $databaseFactory->create();
$this->storage = $storageFactory->create();
$this->filesystem = $filesystemFactory->create();

$storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE);
$this->storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE);

$this->ensureServiceConfigurationValidated('storage', $this->storage);
}
Expand Down
13 changes: 8 additions & 5 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Meanbee\Magedbm2\Application\Config\Option;
use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Exception\ConfigurationException;
use Meanbee\Magedbm2\Service\FilesystemFactory;
use Meanbee\Magedbm2\Service\FilesystemInterface;
use Meanbee\Magedbm2\Service\StorageFactory;
use Meanbee\Magedbm2\Service\StorageInterface;
use Meanbee\Magedbm2\Shell\Command\Gunzip;
use PDO;
Expand Down Expand Up @@ -48,15 +50,16 @@ class ImportCommand extends BaseCommand

/**
* @param ConfigInterface $config
* @throws \Symfony\Component\Console\Exception\LogicException
* @param StorageFactory $storageFactory
* @param FilesystemFactory $filesystemFactory
*/
public function __construct(ConfigInterface $config, StorageInterface $storage, FilesystemInterface $filesystem)
public function __construct(ConfigInterface $config, StorageFactory $storageFactory, FilesystemFactory $filesystemFactory)
{
parent::__construct($config, self::NAME);
$this->storage = $storage;
$this->filesystem = $filesystem;
$this->storage = $storageFactory->create();
$this->filesystem = $filesystemFactory->create();

$storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA);
$this->storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA);

$this->ensureServiceConfigurationValidated('storage', $this->storage);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Command/LsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Exception\ServiceException;
use Meanbee\Magedbm2\Service\Storage\Data\File;
use Meanbee\Magedbm2\Service\StorageFactory;
use Meanbee\Magedbm2\Service\StorageInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -24,14 +25,18 @@ class LsCommand extends BaseCommand
*/
private $dataStorage;

public function __construct(ConfigInterface $config, StorageInterface $storage, StorageInterface $dataStorage)
/**
* @param ConfigInterface $config
* @param StorageFactory $storageFactory
*/
public function __construct(ConfigInterface $config, StorageFactory $storageFactory)
{
parent::__construct($config, self::NAME);

$this->storage = $storage;
$this->storage = $storageFactory->create();
$this->storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE);

$this->dataStorage = $dataStorage;
$this->dataStorage = $storageFactory->create();
$this->dataStorage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA);
}

Expand Down
23 changes: 13 additions & 10 deletions src/Command/PutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Meanbee\Magedbm2\Application\Config\Option;
use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Helper\TableGroupExpander;
use Meanbee\Magedbm2\Service\DatabaseFactory;
use Meanbee\Magedbm2\Service\DatabaseInterface;
use Meanbee\Magedbm2\Service\FilesystemFactory;
use Meanbee\Magedbm2\Service\FilesystemInterface;
use Meanbee\Magedbm2\Exception\ServiceException;
use Meanbee\Magedbm2\Service\StorageFactory;
use Meanbee\Magedbm2\Service\StorageInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -36,25 +39,25 @@ class PutCommand extends BaseCommand

/**
* @param ConfigInterface $config
* @param DatabaseInterface $database
* @param StorageInterface $storage
* @param FilesystemInterface $filesystem
* @param DatabaseFactory $databaseFactory
* @param StorageFactory $storageFactory
* @param FilesystemFactory $filesystemFactory
* @param TableGroupExpander $tableGroupExpander
*/
public function __construct(
ConfigInterface $config,
DatabaseInterface $database,
StorageInterface $storage,
FilesystemInterface $filesystem,
DatabaseFactory $databaseFactory,
StorageFactory $storageFactory,
FilesystemFactory $filesystemFactory,
TableGroupExpander $tableGroupExpander = null
) {
$this->database = $database;
$this->storage = $storage;
$this->filesystem = $filesystem;
$this->database = $databaseFactory->create();
$this->storage = $storageFactory->create();
$this->filesystem = $filesystemFactory->create();
$this->tableExpander = $tableGroupExpander ?? new TableGroupExpander();
$this->config = $config;

$storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE);
$this->storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE);

parent::__construct($config, self::NAME);

Expand Down
9 changes: 7 additions & 2 deletions src/Command/RmCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Meanbee\Magedbm2\Application\ConfigInterface;
use Meanbee\Magedbm2\Exception\ServiceException;
use Meanbee\Magedbm2\Service\StorageFactory;
use Meanbee\Magedbm2\Service\StorageInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -24,11 +25,15 @@ class RmCommand extends BaseCommand
/** @var StorageInterface */
protected $storage;

public function __construct(ConfigInterface $config, StorageInterface $storage)
/**
* @param ConfigInterface $config
* @param StorageFactory $storageFactory
*/
public function __construct(ConfigInterface $config, StorageFactory $storageFactory)
{
parent::__construct($config, self::NAME);

$this->storage = $storage;
$this->storage = $storageFactory->create();

$this->ensureServiceConfigurationValidated('storage', $this->storage);
}
Expand Down
19 changes: 14 additions & 5 deletions src/Service/DatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
class DatabaseFactory
{
/**
* @param Container $container
* @var Container
*/
private $container;

public function __construct(Container $container)
{
$this->container = $container;
}

/**
* @return DatabaseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public static function create(Container $container)
public function create()
{
switch ($container->get('database_adapter')) {
switch ($this->container->get('database_adapter')) {
case 'shell':
default:
$instance = $container->make(\Meanbee\Magedbm2\Service\Database\Shell::class);
$instance = $this->container->make(\Meanbee\Magedbm2\Service\Database\Shell::class);
}

if ($instance instanceof \Psr\Log\LoggerAwareInterface) {
$instance->setLogger($container->get('logger'));
$instance->setLogger($this->container->get('logger'));
}

return $instance;
Expand Down
19 changes: 14 additions & 5 deletions src/Service/FilesystemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
class FilesystemFactory
{
/**
* @param Container $container
* @var Container
*/
private $container;

public function __construct(Container $container)
{
$this->container = $container;
}

/**
* @return FilesystemInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public static function create(Container $container)
public function create()
{
switch ($container->get('filesystem_adapter')) {
switch ($this->container->get('filesystem_adapter')) {
case 'simple':
default:
$instance = $container->make(\Meanbee\Magedbm2\Service\Filesystem\Simple::class);
$instance = $this->container->make(\Meanbee\Magedbm2\Service\Filesystem\Simple::class);
}

if ($instance instanceof \Psr\Log\LoggerAwareInterface) {
$instance->setLogger($container->get('logger'));
$instance->setLogger($this->container->get('logger'));
}

return $instance;
Expand Down
21 changes: 15 additions & 6 deletions src/Service/StorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,33 @@
class StorageFactory
{
/**
* @param Container $container
* @var Container
*/
private $container;

public function __construct(Container $container)
{
$this->container = $container;
}

/**
* @return StorageInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public static function create(Container $container)
public function create()
{
switch ($container->get('storage_adapter')) {
switch ($this->container->get('storage_adapter')) {
case 's3':
$instance = $container->make(\Meanbee\Magedbm2\Service\Storage\S3::class);
$instance = $this->container->make(\Meanbee\Magedbm2\Service\Storage\S3::class);
break;
case 'local':
default:
$instance = $container->make(\Meanbee\Magedbm2\Service\Storage\Local::class);
$instance = $this->container->make(\Meanbee\Magedbm2\Service\Storage\Local::class);
}

if ($instance instanceof \Psr\Log\LoggerAwareInterface) {
$instance->setLogger($container->get('logger'));
$instance->setLogger($this->container->get('logger'));
}

return $instance;
Expand Down
Loading

0 comments on commit 4c9c7f9

Please sign in to comment.