Skip to content

Commit

Permalink
Merge pull request #10 from redco/0.2
Browse files Browse the repository at this point in the history
0.2
  • Loading branch information
maZahaca committed Sep 29, 2015
2 parents 83ca6ca + 6003745 commit b0e7faf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/RedCode/Currency/Rate/Provider/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace RedCode\Currency\Rate\Provider;

/**
* @author maZahaca
*/
class ProviderFactory
{
private $providers;
Expand All @@ -13,14 +10,14 @@ class ProviderFactory
* @param ICurrencyRateProvider[] $providers
* @throws \Exception
*/
public function __construct($providers)
public function __construct($providers = [])
{
if (is_array($providers)) {
foreach ($providers as $provider) {
if (!($provider instanceof ICurrencyRateProvider)) {
throw new \Exception('Provider must be instance of ICurrencyRateProvider');
}
$this->providers[$provider->getName()] = $provider;
$this->addProvider($provider);
}
}
}
Expand All @@ -36,6 +33,18 @@ public function get($name)
}
}

/**
* Add a provider to factory
* @param ICurrencyRateProvider $provider
* @return $this
*/
public function addProvider(ICurrencyRateProvider $provider)
{
$this->providers[$provider->getName()] = $provider;

return $this;
}

/**
* @return ICurrencyRateProvider[]
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/ProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public function testProviderFactoryCreate()
$this->assertNull($factory->get('WrongProviderName'));
}

public function testProviderFactoryAddProvider()
{
$factory = new ProviderFactory();
$factory->addProvider($this->provider);
$provider = $factory->get($this->providerName);

$this->assertInstanceOf(self::PROVIDER_INTERFACE, $provider);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Provider must be instance of ICurrencyRateProvider
Expand Down

0 comments on commit b0e7faf

Please sign in to comment.