1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Rafał Muszyński <[email protected]> |
5
|
|
|
* @copyright 2014 Sourcefabric o.p.s. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.txt |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Newscoop\PaywallBundle\EventListener; |
10
|
|
|
|
11
|
|
|
use Newscoop\PaywallBundle\Entity\Gateway; |
12
|
|
|
use Doctrine\ORM\EntityManager; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Adapters listener. |
16
|
|
|
*/ |
17
|
|
|
class AdaptersListener |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Entity Manager. |
21
|
|
|
* |
22
|
|
|
* @var EntityManager |
23
|
|
|
*/ |
24
|
|
|
private $entityManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Paywall Omnipay config. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct. |
35
|
|
|
* |
36
|
|
|
* @param EntityManager $entityManager |
37
|
|
|
* @param array $config |
38
|
|
|
*/ |
39
|
|
|
public function __construct(EntityManager $entityManager, array $config) |
40
|
|
|
{ |
41
|
|
|
$this->entityManager = $entityManager; |
42
|
|
|
$this->config = $config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Register external adapters. |
47
|
|
|
*/ |
48
|
|
|
public function registerExternalAdapters() |
49
|
|
|
{ |
50
|
|
|
$this->installAdapters(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Install external adapters. |
55
|
|
|
*/ |
56
|
|
|
private function installAdapters() |
57
|
|
|
{ |
58
|
|
|
if (!isset($this->config['gateways'])) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$activeAdapter = $this->entityManager->getRepository('Newscoop\PaywallBundle\Entity\Gateway') |
63
|
|
|
->findOneByisActive(true); |
64
|
|
|
|
65
|
|
|
foreach ((array) $this->config['gateways'] as $name => $gateway) { |
66
|
|
|
$adapter = $this->entityManager->getRepository('Newscoop\PaywallBundle\Entity\Gateway') |
67
|
|
|
->findOneByValue($name); |
68
|
|
|
|
69
|
|
|
if (!$adapter) { |
70
|
|
|
$adapter = new Gateway(); |
71
|
|
|
$adapter->setName($name); |
72
|
|
|
$adapter->setValue($name); |
73
|
|
|
|
74
|
|
|
if ($activeAdapter && $name !== $activeAdapter->getValue()) { |
75
|
|
|
$adapter->setActive(false); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->entityManager->persist($adapter); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->entityManager->flush(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|