Skip to content

Commit 835bff5

Browse files
author
Cédric Girard
committed
Merge pull request #37 from poschi/master
Removed direct/hard dependency to database
2 parents 144b583 + 83d5355 commit 835bff5

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

Diff for: Adapter/AbstractCurrencyAdapter.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ abstract class AbstractCurrencyAdapter extends \ArrayIterator
1919
*/
2020
protected $managedCurrencies = array();
2121

22+
/**
23+
* @var string
24+
*/
25+
protected $currencyClass;
26+
2227
/**
2328
* Set default currency
2429
*
@@ -75,7 +80,7 @@ public function setCurrencyClass($currencyClass)
7580
* @param mixed $index
7681
* @param Currency $newval
7782
*/
78-
public function offsetSet ($index, $newval)
83+
public function offsetSet($index, $newval)
7984
{
8085
if (!$newval instanceof $this->currencyClass) {
8186
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($newval)));

Diff for: Adapter/AdapterFactory.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,10 @@ public function createDoctrineAdapter($adapterClass = null, $entityManagerName =
6969
if (null == $adapterClass) {
7070
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter';
7171
}
72-
7372
$adapter = $this->create($adapterClass);
7473

7574
$em = $this->doctrine->getManager($entityManagerName);
76-
77-
$currencies = $em
78-
->getRepository($this->currencyClass)
79-
->findAll();
80-
81-
foreach ($currencies as $currency) {
82-
$adapter[$currency->getCode()] = $currency;
83-
}
75+
$adapter->setManager($em);
8476

8577
return $adapter;
8678
}

Diff for: Adapter/DoctrineCurrencyAdapter.php

+61
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22

33
namespace Lexik\Bundle\CurrencyBundle\Adapter;
44

5+
use Doctrine\ORM\EntityManager;
6+
use Exception;
7+
58
/**
69
* @author Yoann Aparici <[email protected]>
710
* @author Cédric Girard <[email protected]>
811
*/
912
class DoctrineCurrencyAdapter extends AbstractCurrencyAdapter
1013
{
14+
/**
15+
* @var ObjectManager
16+
*/
17+
private $manager;
18+
19+
/**
20+
* @var bool
21+
*/
22+
private $initialized = false;
23+
1124
/**
1225
* {@inheritdoc}
1326
*/
@@ -25,4 +38,52 @@ public function getIdentifier()
2538
{
2639
return 'doctrine';
2740
}
41+
42+
/**
43+
* @param EntityManager $manager
44+
*/
45+
public function setManager(EntityManager $manager)
46+
{
47+
$this->manager = $manager;
48+
}
49+
50+
public function offsetExists($index)
51+
{
52+
if (!$this->isInitialized()) {
53+
$this->initialize();
54+
}
55+
return parent::offsetExists($index);
56+
}
57+
58+
public function offsetGet($index)
59+
{
60+
if (!$this->isInitialized()) {
61+
$this->initialize();
62+
}
63+
64+
return parent::offsetGet($index);
65+
}
66+
67+
/**
68+
* @return bool
69+
*/
70+
private function isInitialized()
71+
{
72+
return $this->initialized;
73+
}
74+
75+
private function initialize()
76+
{
77+
if (!isset($this->manager)) {
78+
throw new Exception('No ObjectManager set');
79+
}
80+
81+
$currencies = $this->manager
82+
->getRepository($this->currencyClass)
83+
->findAll();
84+
85+
foreach ($currencies as $currency) {
86+
$this[$currency->getCode()] = $currency;
87+
}
88+
}
2889
}

0 commit comments

Comments
 (0)