Skip to content

Commit

Permalink
Added ProviderFactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jan 23, 2016
1 parent 1b17c16 commit 45ca9dd
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 30 deletions.
15 changes: 9 additions & 6 deletions README.md
@@ -1,9 +1,10 @@
Porter
======

[![Version][Version image]][Releases]
[![Latest version][Version image]][Releases]
[![Build status][Build image]][Build]
[![Coverage][Coverage image]][Coverage]
[![Test coverage][Coverage image]][Coverage]
[![Code style][Style image]][Style]

Porter is a data import abstraction library.

Expand All @@ -14,8 +15,10 @@ Requirements


[Releases]: https://github.com/ScriptFUSION/Porter/releases
[Version image]: http://img.shields.io/github/tag/ScriptFUSION/Porter.svg "Latest version"
[Version image]: https://poser.pugx.org/scriptfusion/porter/v/stable "Latest version"
[Build]: http://travis-ci.org/ScriptFUSION/Porter
[Build image]: http://img.shields.io/travis/ScriptFUSION/Porter.svg "Build status"
[Coverage]: https://coveralls.io/github/ScriptFUSION/Porter?branch=master
[Coverage image]: https://coveralls.io/repos/ScriptFUSION/Porter/badge.svg?branch=master&service=github "Test coverage"
[Build image]: https://travis-ci.org/ScriptFUSION/Porter.svg "Build status"
[Coverage]: https://coveralls.io/github/ScriptFUSION/Porter
[Coverage image]: https://coveralls.io/repos/ScriptFUSION/Porter/badge.svg "Test coverage"
[Style]: https://styleci.io/repos/49824895
[Style image]: https://styleci.io/repos/49824895/shield?style=flat "Code style"
10 changes: 10 additions & 0 deletions src/Porter/ObjectNotCreatedException.php
@@ -0,0 +1,10 @@
<?php
namespace ScriptFUSION\Porter;

/**
* The exception that is thrown when an object could not be created.
*/
class ObjectNotCreatedException extends \RuntimeException
{
// Intentionally empty.
}
24 changes: 23 additions & 1 deletion src/Porter/Porter.php
Expand Up @@ -8,11 +8,15 @@
use ScriptFUSION\Porter\Mapping\Mapping;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\ProviderData;
use ScriptFUSION\Porter\Provider\ProviderFactory;
use ScriptFUSION\Porter\Specification\ImportSpecification;

class Porter
{
private $providers;

private $providerFactory;

private $mapper;

/**
Expand Down Expand Up @@ -51,7 +55,13 @@ public function getProvider($name)
return $this->providers["$name"];
}

throw new ProviderNotFoundException("No such provider registered: \"$name\".");
try {
$this->addProvider($provider = $this->getOrCreateProviderFactory()->createProvider("$name"));
} catch (ObjectNotCreatedException $e) {
throw new ProviderNotFoundException("No such provider registered: \"$name\".", $e);
}

return $provider;
}

public function addProvider(Provider $provider)
Expand All @@ -70,6 +80,18 @@ public function addProviders(array $providers)
return $this;
}

private function getOrCreateProviderFactory()
{
return $this->providerFactory ?: $this->providerFactory = new ProviderFactory;
}

public function setProviderFactory(ProviderFactory $factory)
{
$this->providerFactory = $factory;

return $this;
}

/**
* @return Mapper
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Porter/Provider/ProviderFactory.php
@@ -0,0 +1,17 @@
<?php
namespace ScriptFUSION\Porter\Provider;

use ScriptFUSION\Porter\ObjectNotCreatedException;

class ProviderFactory
{
public function createProvider($name)
{
switch ($name) {
case StaticDataProvider::class:
return new StaticDataProvider;
}

throw new ObjectNotCreatedException("Factory does not know how to create: \"$name\".");
}
}
7 changes: 6 additions & 1 deletion src/Porter/ProviderNotFoundException.php
@@ -1,7 +1,12 @@
<?php
namespace ScriptFUSION\Porter;

use Exception;

final class ProviderNotFoundException extends \RuntimeException
{
// Intentionally empty.
public function __construct($message, Exception $previous = null)
{
parent::__construct($message, 0, $previous);
}
}
@@ -1,7 +1,8 @@
<?php
namespace ScriptFUSION\Porter;
namespace ScriptFUSION\Porter\Specification;

use ScriptFUSION\Porter\Mapping\Mapping;
use ScriptFUSION\Porter\ObjectFinalizedException;
use ScriptFUSION\Porter\Provider\ProviderData;

class ImportSpecification
Expand Down
12 changes: 12 additions & 0 deletions src/Porter/Specification/StaticDataImportSpecification.php
@@ -0,0 +1,12 @@
<?php
namespace ScriptFUSION\Porter\Specification;

use ScriptFUSION\Porter\Provider\StaticData;

class StaticDataImportSpecification extends ImportSpecification
{
public function __construct(\Iterator $data)
{
parent::__construct(new StaticData($data));
}
}
15 changes: 15 additions & 0 deletions test/Functional/StaticDataTest.php
@@ -0,0 +1,15 @@
<?php
namespace ScriptFUSIONTest\Functional;

use ScriptFUSION\Porter\Porter;
use ScriptFUSION\Porter\Specification\StaticDataImportSpecification;

final class StaticDataTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$records = (new Porter)->import(new StaticDataImportSpecification(new \ArrayIterator(['foo'])));

$this->assertSame('foo', $records->current());
}
}
@@ -1,13 +1,13 @@
<?php
namespace ScriptFUSIONTest\Unit\Porter;
namespace ScriptFUSIONTest\Integration\Porter;

use Mockery\MockInterface;
use ScriptFUSION\Porter\Collection\ProviderRecords;
use ScriptFUSION\Porter\ImportSpecification;
use ScriptFUSION\Porter\Porter;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\ProviderData;
use ScriptFUSION\Porter\ProviderNotFoundException;
use ScriptFUSION\Porter\Specification\ImportSpecification;

final class PorterTest extends \PHPUnit_Framework_TestCase
{
Expand Down
18 changes: 0 additions & 18 deletions test/Integration/Porter/Provider/StaticDataProviderTest.php

This file was deleted.

12 changes: 12 additions & 0 deletions test/Unit/Porter/Connector/NullConnectorTest.php
@@ -0,0 +1,12 @@
<?php
namespace ScriptFUSIONTest\Unit\Porter\Connector;

use ScriptFUSION\Porter\Connector\NullConnector;

final class NullConnectorTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$this->assertNull((new NullConnector)->fetch('foo'));
}
}
2 changes: 1 addition & 1 deletion test/Unit/Porter/ImportSpecificationTest.php
@@ -1,10 +1,10 @@
<?php
namespace ScriptFUSIONTest\Unit\Porter;

use ScriptFUSION\Porter\ImportSpecification;
use ScriptFUSION\Porter\Mapping\Mapping;
use ScriptFUSION\Porter\ObjectFinalizedException;
use ScriptFUSION\Porter\Provider\ProviderData;
use ScriptFUSION\Porter\Specification\ImportSpecification;

final class ImportSpecificationTest extends \PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit 45ca9dd

Please sign in to comment.