-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.js
209 lines (163 loc) · 6.02 KB
/
factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const config = require('./data/config.json');
const constants = require('./constants.json');
const path = require('path');
const DebugLogger = require('./util/DebugLogger');
const ProductRepository = require('./datamodel/ProductRepository');
const OrderRepository = require('./datamodel/OrderRepository');
const DownloadLog = require('./datamodel/DownloadLog');
const ExchangeRateRepository = require('./datamodel/ExchangeRateRepository');
const CheckoutPageBuilder = require('./domain/CheckoutPageBuilder');
const DownloadPageBuilder = require('./domain/DownloadPageBuilder');
const ErrorPageBuilder = require('./domain/ErrorPageBuilder');
const PaymentVerifier = require('./domain/PaymentVerifier');
const DownloadManager = require('./domain/DownloadManager');
const BrainBlocksMarkupGenerator = require('./domain/NANO/BrainBlocksMarkupGenerator');
const BrainBlocksPaymentVerifier = require('./domain/NANO/BrainBlocksPaymentVerifier');
const NanoAddressRepository = require("./domain/NANO/NanoAddressRepository");
const BITBOXSDK = require('bitbox-sdk').BITBOX;
const BitcoinCashAddressRepository = require("./domain/BCH/BitcoinCashAddressRepository");
const BitcoinCashPaymentVerifier = require("./domain/BCH/BitcoinCashPaymentVerifier");
const BitcoinCashMarkupGenerator = require("./domain/BCH/BitcoinCashMarkupGenerator");
class Factory {
GetDebugLogger() {
if (!this.debugLogger) {
this.debugLogger = new DebugLogger();
}
return this.debugLogger;
}
GetProductRepository() {
if (!this.productRepository) {
this.productRepository = new ProductRepository(path.resolve(config.DBPath), this.GetDebugLogger());
}
return this.productRepository;
}
GetOrderRepository() {
if (!this.orderRepository) {
this.orderRepository = new OrderRepository(path.resolve(config.DBPath), this.GetDebugLogger());
}
return this.orderRepository;
}
GetDownloadLog() {
if (!this.downloadLog) {
this.downloadLog = new DownloadLog(path.resolve(config.DBPath));
}
return this.downloadLog;
}
GetExchangeRateRepository() {
if (!this.exchangeRateRepository) {
this.exchangeRateRepository = new ExchangeRateRepository(path.resolve(config.DBPath), this.GetDebugLogger());
}
return this.exchangeRateRepository;
}
GetCheckoutPageBuilder(coinType) {
let coinSpecificMarkupGenerator;
let coinSpecificAddressRepository;
switch (coinType) {
case constants.COIN_TYPE_NANO:
coinSpecificMarkupGenerator = this.GetNanoCheckoutMarkupGenerator();
coinSpecificAddressRepository = this.GetNanoAddressRepository();
break;
case constants.COIN_TYPE_BCH:
coinSpecificMarkupGenerator = this.GetBitcoinCashCheckoutMarkupGenerator();
coinSpecificAddressRepository = this.GetBitcoinCashAddressRepository();
break;
default:
throw 'Coin type "' + coinType + '" not recognised.';
}
if (!this.checkoutPageBuilder) {
this.checkoutPageBuilder = {};
}
if (!this.checkoutPageBuilder[coinType]) {
this.checkoutPageBuilder[coinType] = new CheckoutPageBuilder(
this.GetProductRepository(),
this.GetOrderRepository(),
this.GetExchangeRateRepository(),
this.GetDebugLogger(),
coinSpecificMarkupGenerator,
coinSpecificAddressRepository
);
}
return this.checkoutPageBuilder[coinType];
}
GetDownloadPageBuilder() {
if (!this.downloadPageBuilder) {
this.downloadPageBuilder = new DownloadPageBuilder(this.GetProductRepository(), this.GetOrderRepository(), this.GetDownloadLog(), this.GetDebugLogger());
}
return this.downloadPageBuilder;
}
GetErrorPageBuilder() {
if (!this.errorPageBuilder) {
this.errorPageBuilder = new ErrorPageBuilder();
}
return this.errorPageBuilder;
}
GetNanoCheckoutMarkupGenerator() {
if (!this.brainBlocksMarkupGenerator) {
this.brainBlocksMarkupGenerator = new BrainBlocksMarkupGenerator();
}
return this.brainBlocksMarkupGenerator;
}
GetNanoAddressRepository() {
if (!this.nanoAddressRepository) {
this.nanoAddressRepository = new NanoAddressRepository();
}
return this.nanoAddressRepository;
}
GetPaymentVerifier(coinType) {
let coinSpecificPaymentVerifier;
switch (coinType) {
case constants.COIN_TYPE_NANO:
coinSpecificPaymentVerifier = this.GetBrainBlocksPaymentVerifier();
break;
case constants.COIN_TYPE_BCH:
coinSpecificPaymentVerifier = this.GetBitcoinCashPaymentVerifier();
break;
default:
throw 'Coin type "' + coinType + '" not recognised.';
}
if (!this.paymentVerifier) {
this.paymentVerifier = {};
}
if (!this.paymentVerifier[coinType]) {
this.paymentVerifier[coinType] = new PaymentVerifier(this.GetDownloadManager(), coinSpecificPaymentVerifier);
}
return this.paymentVerifier[coinType];
}
GetBrainBlocksPaymentVerifier() {
if (!this.brainBlocksPaymentVerifier) {
this.brainBlocksPaymentVerifier = new BrainBlocksPaymentVerifier(this.GetOrderRepository(), this.GetDebugLogger());
}
return this.brainBlocksPaymentVerifier;
}
GetDownloadManager() {
if (!this.downloadManager) {
this.downloadManager = new DownloadManager(this.GetDownloadLog(), this.GetOrderRepository(), this.GetProductRepository(), this.GetDebugLogger());
}
return this.downloadManager;
}
GetBitcoinCashPaymentVerifier() {
if (!this.bitcoinCashPaymentVerifier) {
this.bitcoinCashPaymentVerifier = new BitcoinCashPaymentVerifier(this.GetOrderRepository(), this.GetDebugLogger());
}
return this.bitcoinCashPaymentVerifier;
}
GetBitcoinCashAddressRepository() {
if (!this.bitcoinCashAddressRepository) {
this.bitcoinCashAddressRepository = new BitcoinCashAddressRepository(path.resolve(config.DBPath), this.GetDebugLogger(), this.GetBITBOX());
}
return this.bitcoinCashAddressRepository;
}
GetBitcoinCashCheckoutMarkupGenerator() {
if (!this.bitcoinCashCheckoutMarkupGenerator) {
this.bitcoinCashCheckoutMarkupGenerator = new BitcoinCashMarkupGenerator();
}
return this.bitcoinCashCheckoutMarkupGenerator;
}
GetBITBOX() {
if (!this.BITBOX) {
this.BITBOX = new BITBOXSDK({ restURL: `https://rest.bitcoin.com/v2/` });
}
return this.BITBOX;
}
}
module.exports = Factory;