Completed
Push — master ( 919a96...568a86 )
by wiese
73:55 queued 09:12
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer;
6
7
use RuntimeException;
8
use Sofort\SofortLib\Sofortueberweisung;
9
10
/**
11
 * Facade in front of Sofortueberweisung, an API to generate URLs of Sofort's checkout process
12
 */
13
class Client {
14
15
	/**
16
	 * @var Sofortueberweisung
17
	 */
18
	private $api;
19
20
	public function __construct( string $configkey ) {
21
		$this->api = new Sofortueberweisung( $configkey );
22
	}
23
24
	/**
25
	 * Set API to use instead of the one chosen by the facade
26
	 */
27
	public function setApi( Sofortueberweisung $sofortueberweisung ): void {
28
		$this->api = $sofortueberweisung;
29
	}
30
31
	/**
32
	 * Perform the given request and return a response
33
	 *
34
	 * @throws RuntimeException
35
	 */
36
	public function get( Request $request ): Response {
37
38
		// Mapping currency amount to 3rd party float format. Known flaw
39
		$this->api->setAmount( $request->getAmount()->getEuroFloat() );
40
41
		$this->api->setCurrencyCode( $request->getCurrencyCode() );
42
43
		$reasons = $request->getReasons();
44
		$this->api->setReason( $reasons[0] ?? '', $reasons[1] ?? '' );
45
46
		$this->api->setSuccessUrl( $request->getSuccessUrl(), true );
47
		$this->api->setAbortUrl( $request->getAbortUrl() );
48
		$this->api->setNotificationUrl( $request->getNotificationUrl() );
49
50
		$this->api->sendRequest();
51
52
		if ( $this->api->isError() ) {
53
			throw new RuntimeException( $this->api->getError() );
54
		}
55
56
		$response = new Response();
57
		$response->setPaymentUrl( $this->api->getPaymentUrl() );
58
		$response->setTransactionId( $this->api->getTransactionId() );
59
60
		return $response;
61
	}
62
}
63