core   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 32
c 7
b 1
f 0
dl 0
loc 108
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A notify_donation_errors() 0 3 1
A send_notification() 0 4 1
A notify_donor_donation_received() 0 3 1
A notify_admin_donation_received() 0 3 1
A get_notification_data() 0 22 2
A get_formatted_net_amount() 0 13 2
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2024 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\notification;
12
13
use phpbb\notification\manager;
14
use skouat\ppde\actions\currency;
15
use skouat\ppde\entity\transactions;
16
17
class core
18
{
19
	protected $notification_manager;
20
	protected $actions_currency;
21
	protected $entity_transaction;
22
23
	/**
24
	 * Constructor
25
	 *
26
	 * @param manager      $notification_manager Notification object
27
	 * @param currency     $actions_currency     Currency actions object
28
	 * @param transactions $entity_transaction   Transaction entity object
29
	 */
30
	public function __construct(
31
		manager $notification_manager,
32
		currency $actions_currency,
33
		transactions $entity_transaction
34
	)
35
	{
36
		$this->notification_manager = $notification_manager;
37
		$this->actions_currency = $actions_currency;
38
		$this->entity_transaction = $entity_transaction;
39
	}
40
41
	/**
42
	 * Notify admin when the donation contains errors
43
	 */
44
	public function notify_donation_errors(): void
45
	{
46
		$this->send_notification('skouat.ppde.notification.type.admin_donation_errors', 'donation_errors');
47
	}
48
49
	/**
50
	 * Notify admin when the donation is received
51
	 */
52
	public function notify_admin_donation_received(): void
53
	{
54
		$this->send_notification('skouat.ppde.notification.type.admin_donation_received');
55
	}
56
57
	/**
58
	 * Notify donor when the donation is received
59
	 */
60
	public function notify_donor_donation_received(): void
61
	{
62
		$this->send_notification('skouat.ppde.notification.type.donor_donation_received');
63
	}
64
65
	/**
66
	 * Send notification
67
	 *
68
	 * @param string $notification_type
69
	 * @param string $donation_type
70
	 */
71
	private function send_notification(string $notification_type, string $donation_type = ''): void
72
	{
73
		$notification_data = $this->get_notification_data($donation_type);
74
		$this->notification_manager->add_notifications($notification_type, $notification_data);
75
	}
76
77
	/**
78
	 * Get notification data
79
	 *
80
	 * @param string $donation_type
81
	 * @return array
82
	 */
83
	private function get_notification_data(string $donation_type = ''): array
84
	{
85
		$notification_data = [
86
			'transaction_id' => $this->entity_transaction->get_id(),
87
			'txn_id'         => $this->entity_transaction->get_txn_id(),
88
			'user_from'      => $this->entity_transaction->get_user_id(),
89
			'payer_email'    => $this->entity_transaction->get_payer_email(),
90
			'payer_username' => $this->entity_transaction->get_username(),
91
		];
92
93
		if ($donation_type === 'donation_errors')
94
		{
95
			$notification_data['txn_errors'] = $this->entity_transaction->get_txn_errors();
96
		}
97
		else
98
		{
99
			$this->actions_currency->set_currency_data_from_iso_code($this->entity_transaction->get_mc_currency());
100
			$notification_data['mc_gross'] = $this->actions_currency->format_currency($this->entity_transaction->get_mc_gross());
101
			$notification_data['net_amount'] = $this->get_formatted_net_amount();
102
		}
103
104
		return $notification_data;
105
	}
106
107
	/**
108
	 * Get formatted net amount
109
	 *
110
	 * @return string
111
	 */
112
	private function get_formatted_net_amount(): string
113
	{
114
		$settle_amount = $this->entity_transaction->get_settle_amount();
115
		if ($settle_amount > 0)
116
		{
117
			$this->actions_currency->set_currency_data_from_iso_code($this->entity_transaction->get_settle_currency());
118
		}
119
		else
120
		{
121
			$settle_amount = $this->entity_transaction->get_net_amount();
122
			$this->actions_currency->set_currency_data_from_iso_code($this->entity_transaction->get_mc_currency());
123
		}
124
		return $this->actions_currency->format_currency($settle_amount);
125
	}
126
}
127