Skip to content

Commit fd470da

Browse files
committed
Refactor some code
1 parent 6afe2a0 commit fd470da

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

src/API.php

+54-34
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public function __construct()
2929
'timeout' => 60,
3030
'cookies' => true,
3131
'headers' => [
32-
'User-Agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"
33-
]
32+
'User-Agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36" ]
3433
));
3534

3635
$this->client->setClient($this->guzzleClient);
@@ -75,12 +74,15 @@ public function login($username, $password)
7574

7675
$bal = $balance->count() ? $balance->text() : 0;
7776
$avl = $available->count() ? $available->text() : 0;
77+
$bsb = $this->processNumbersOnly($bsb->count() ? $bsb->text() : '');
78+
$accountNumber = $this->processNumbersOnly($accountNumber->count() ? $accountNumber->text() : '');
79+
$fullAccountNumber = $bsb . $accountNumber;
7880

79-
$accountList[] = [
81+
$accountList[$fullAccountNumber] = [
8082
'nickname' => $name->text(),
8183
'url' => $name->attr('href'),
82-
'bsb' => $bsb->count() ? $bsb->text() : '',
83-
'accountNum' => $accountNumber->count() ? $accountNumber->text() : '',
84+
'bsb' => $bsb,
85+
'accountNum' => $accountNumber,
8486
'balance' => $this->processCurrency($bal),
8587
'available' => $this->processCurrency($avl)
8688
];
@@ -95,8 +97,7 @@ public function login($username, $password)
9597

9698
public function getTransactions($account, $from, $to)
9799
{
98-
$link = sprintf("%s%s", self::BASE_URL, $account['url']);
99-
$crawler = $this->client->request('GET', $link);
100+
$crawler = $this->getAccountPage($account);
100101

101102
$form = $crawler->filter('#aspnetForm');
102103

@@ -110,6 +111,9 @@ public function getTransactions($account, $from, $to)
110111
$field = $this->createField('input', '__EVENTTARGET', 'ctl00$BodyPlaceHolder$lbSearch');
111112
$form->set($field);
112113

114+
$field = $this->createField('input', '__EVENTARGUMENT', '');
115+
$form->set($field);
116+
113117
$field = $this->createField('input', 'ctl00$ctl00', 'ctl00$BodyPlaceHolder$updatePanelSearch|ctl00$BodyPlaceHolder$lbSearch');
114118
$form->set($field);
115119

@@ -132,8 +136,44 @@ public function getTransactions($account, $from, $to)
132136
$form->set($field);
133137

134138
$crawler = $this->client->submit($form);
135-
$html = $crawler->html();
136139

140+
return $this->filterTransactions($crawler);
141+
}
142+
143+
public function setTimezone($timezone)
144+
{
145+
$this->timezone = $timezone;
146+
}
147+
148+
private function processNumbersOnly($value)
149+
{
150+
return preg_replace('$[^0-9]$', '', $value);
151+
}
152+
153+
private function processCurrency($amount)
154+
{
155+
$value = preg_replace('$[^0-9.]$', '', $amount);
156+
157+
if (strstr($amount, 'DR')) {
158+
$value = -$value;
159+
}
160+
161+
return $value;
162+
}
163+
164+
private function createField($type, $name, $value)
165+
{
166+
$domdocument = new \DOMDocument;
167+
$ff = $domdocument->createElement($type);
168+
$ff->setAttribute('name', $name);
169+
$ff->setAttribute('value', $value);
170+
$formfield = new InputFormField($ff);
171+
172+
return $formfield;
173+
}
174+
175+
public function filterTransactions($crawler)
176+
{
137177
$pattern = '
138178
/
139179
\{ # { character
@@ -145,10 +185,10 @@ public function getTransactions($account, $from, $to)
145185
\} # } character
146186
/x
147187
';
188+
$html = $crawler->html();
148189

149-
$extracted = preg_match_all($pattern, $html, $matches);
190+
preg_match_all($pattern, $html, $matches);
150191

151-
$transactions = [];
152192
foreach ($matches[0] as $_temp) {
153193
if (strstr($_temp, '{"Transactions"')) {
154194
$transactions = json_decode($_temp);
@@ -171,35 +211,15 @@ public function getTransactions($account, $from, $to)
171211
'receiptnumber' => $transaction->ReceiptNumber->Text,
172212
];
173213
}
174-
}
175-
176-
return $transactionList;
177-
}
178-
179-
public function setTimezone($timezone)
180-
{
181-
$this->timezone = $timezone;
182-
}
183-
184-
private function processCurrency($amount)
185-
{
186-
$value = preg_replace('$[^0-9.]$', '', $amount);
187214

188-
if (strstr($amount, 'DR')) {
189-
$value = -$value;
190215
}
191216

192-
return $value;
217+
return $transactionList;
193218
}
194219

195-
private function createField($type, $name, $value)
220+
private function getAccountPage($account)
196221
{
197-
$domdocument = new \DOMDocument;
198-
$ff = $domdocument->createElement($type);
199-
$ff->setAttribute('name', $name);
200-
$ff->setAttribute('value', $value);
201-
$formfield = new InputFormField($ff);
202-
203-
return $formfield;
222+
$link = sprintf("%s%s", self::BASE_URL, $account['url']);
223+
return $this->client->request('GET', $link);
204224
}
205225
}

0 commit comments

Comments
 (0)