Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

AllianceBank::build()   F
last analyzed

Complexity

Conditions 14
Paths 384

Size

Total Lines 117
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 80
nc 384
nop 2
dl 0
loc 117
rs 2.943
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Player\Bank;
4
5
use AbstractSmrPlayer;
6
use Menu;
7
use Smr\Database;
8
use Smr\Epoch;
9
use Smr\Page\PlayerPage;
10
use Smr\Session;
11
use Smr\Template;
12
use SmrAlliance;
13
use SmrPlayer;
14
15
class AllianceBank extends PlayerPage {
16
17
	public string $file = 'bank_alliance.php';
18
19
	public function __construct(
20
		private readonly int $allianceID,
21
	) {}
22
23
	public function build(AbstractSmrPlayer $player, Template $template): void {
24
		$session = Session::getInstance();
25
26
		// is account validated?
27
		if (!$player->getAccount()->isValidated()) {
28
			create_error('You are not validated so you cannot use banks.');
29
		}
30
31
		$allianceID = $this->allianceID;
32
33
		$alliance = SmrAlliance::getAlliance($allianceID, $player->getGameID());
34
		$template->assign('PageTopic', 'Bank');
35
36
		Menu::bank();
37
38
		$db = Database::getInstance();
39
		$dbResult = $db->read('SELECT * FROM alliance_treaties WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . '
40
					AND (alliance_id_1 = ' . $db->escapeNumber($player->getAllianceID()) . ' OR alliance_id_2 = ' . $db->escapeNumber($player->getAllianceID()) . ')
41
					AND aa_access = 1 AND official = \'TRUE\'');
42
		$alliedAllianceBanks = [];
43
		foreach ($dbResult->records() as $dbRecord) {
44
			$alliedAllianceBanks[$dbRecord->getInt('alliance_id_2')] = SmrAlliance::getAlliance($dbRecord->getInt('alliance_id_2'), $alliance->getGameID());
45
			$alliedAllianceBanks[$dbRecord->getInt('alliance_id_1')] = SmrAlliance::getAlliance($dbRecord->getInt('alliance_id_1'), $alliance->getGameID());
46
		}
47
		$template->assign('AlliedAllianceBanks', $alliedAllianceBanks);
48
49
		$dbResult = $db->read('SELECT transaction, sum(amount) as total FROM alliance_bank_transactions
50
					WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . ' AND payee_id = ' . $db->escapeNumber($player->getAccountID()) . '
51
					GROUP BY transaction');
52
		$playerTrans = ['Deposit' => 0, 'Payment' => 0];
53
		foreach ($dbResult->records() as $dbRecord) {
54
			$playerTrans[$dbRecord->getString('transaction')] = $dbRecord->getInt('total');
55
		}
56
57
		if ($alliance->getAllianceID() == $player->getAllianceID()) {
58
			$role_id = $player->getAllianceRole($alliance->getAllianceID());
59
			$query = 'role_id = ' . $db->escapeNumber($role_id);
60
		} else {
61
			$query = 'role = ' . $db->escapeString($player->getAlliance()->getAllianceName());
62
		}
63
64
		$dbResult = $db->read('SELECT * FROM alliance_has_roles WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . ' AND ' . $query);
65
		$dbRecord = $dbResult->record();
66
		$template->assign('CanExempt', $dbRecord->getBoolean('exempt_with'));
67
		$withdrawalPerDay = $dbRecord->getInt('with_per_day');
68
69
		if ($dbRecord->getBoolean('positive_balance')) {
70
			$template->assign('PositiveWithdrawal', $withdrawalPerDay + $playerTrans['Deposit'] - $playerTrans['Payment']);
71
		} elseif ($withdrawalPerDay == ALLIANCE_BANK_UNLIMITED) {
72
			$template->assign('UnlimitedWithdrawal', true);
73
		} else {
74
			$dbResult = $db->read('SELECT IFNULL(sum(amount), 0) as total FROM alliance_bank_transactions WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . '
75
						AND payee_id = ' . $db->escapeNumber($player->getAccountID()) . ' AND transaction = \'Payment\' AND exempt = 0 AND time > ' . $db->escapeNumber(Epoch::time() - 86400));
76
			$totalWithdrawn = $dbResult->record()->getInt('total');
77
			$template->assign('WithdrawalPerDay', $withdrawalPerDay);
78
			$template->assign('RemainingWithdrawal', $withdrawalPerDay - $totalWithdrawn);
79
			$template->assign('TotalWithdrawn', $totalWithdrawn);
80
		}
81
82
		$maxValue = $session->getRequestVarInt('maxValue', 0);
83
		$minValue = $session->getRequestVarInt('minValue', 0);
84
85
		if ($maxValue <= 0) {
86
			$dbResult = $db->read('SELECT IFNULL(MAX(transaction_id), 0) as max_transaction_id FROM alliance_bank_transactions
87
						WHERE game_id=' . $db->escapeNumber($alliance->getGameID()) . '
88
						AND alliance_id=' . $db->escapeNumber($alliance->getAllianceID()));
89
			$maxValue = $dbResult->record()->getInt('max_transaction_id');
90
		}
91
92
		if ($minValue <= 0 || $minValue > $maxValue) {
93
			$minValue = max(1, $maxValue - 5);
94
		}
95
96
		$query = 'SELECT time, transaction_id, transaction, amount, exempt, reason, payee_id
97
			FROM alliance_bank_transactions
98
			WHERE game_id=' . $db->escapeNumber($alliance->getGameID()) . '
99
			AND alliance_id=' . $db->escapeNumber($alliance->getAllianceID());
100
101
		$query .= ' AND transaction_id>=' . $db->escapeNumber($minValue) . '
102
					AND transaction_id<=' . $db->escapeNumber($maxValue) . '
103
					ORDER BY time LIMIT ' . (1 + $maxValue - $minValue);
104
105
		$dbResult = $db->read($query);
106
107
		// only if we have at least one result
108
		if ($dbResult->hasRecord()) {
109
			$bankTransactions = [];
110
			foreach ($dbResult->records() as $dbRecord) {
111
				$trans = $dbRecord->getString('transaction');
112
				$bankTransactions[$dbRecord->getInt('transaction_id')] = [
113
					'Time' => $dbRecord->getInt('time'),
114
					'Player' => SmrPlayer::getPlayer($dbRecord->getInt('payee_id'), $player->getGameID()),
115
					'Reason' => $dbRecord->getString('reason'),
116
					'TransactionType' => $trans,
117
					'Withdrawal' => $trans == 'Payment' ? number_format($dbRecord->getInt('amount')) : '',
118
					'Deposit' => $trans == 'Deposit' ? number_format($dbRecord->getInt('amount')) : '',
119
					'Exempt' => $dbRecord->getInt('exempt') == 1,
120
				];
121
			}
122
			$template->assign('BankTransactions', $bankTransactions);
123
124
			$template->assign('MinValue', $minValue);
125
			$template->assign('MaxValue', $maxValue);
126
			$container = new self($allianceID);
127
			$template->assign('FilterTransactionsFormHREF', $container->href());
128
129
			$container = new AllianceBankExemptProcessor($minValue, $maxValue);
130
			$template->assign('ExemptTransactionsFormHREF', $container->href());
131
132
			$template->assign('Alliance', $alliance);
133
		}
134
135
		$container = new AllianceBankReport($allianceID);
136
		$template->assign('BankReportHREF', $container->href());
137
138
		$container = new AllianceBankProcessor($allianceID);
139
		$template->assign('BankTransactionFormHREF', $container->href());
140
	}
141
142
}
143