PayController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 69
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setUserFees() 0 18 3
A index() 0 24 4
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Fee;
6
use App\Entity\Transaction;
7
use App\Service\AlmaApi;
8
use App\Service\AlmaUserData;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
 * Displays payments about to be sent to the payment processor.
15
 */
16
17
class PayController extends Controller
18
{
19
    private $api;
20
    private $userData;
21
22
    public function __construct(AlmaApi $api, AlmaUserData $userData)
23
    {
24
        $this->api = $api;
25
        $this->userData = $userData;
26
    }
27
28
    /**
29
     * @Route("/pay", name="payment_handler")
30
     * @param Request $request
31
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
32
     * @throws \GuzzleHttp\Exception\GuzzleException
33
     */
34
    public function index(Request $request)
35
    {
36
        $feeIds = $request->request->get('fee');
37
        if (!isset($feeIds) || !count($feeIds)) {
38
            return $this->redirectToRoute('index');
39
        }
40
41
        $transaction = new Transaction($this->getUser()->getUsername());
42
43
        $entityManager = $this->getDoctrine()->getManager();
44
        if ($this->setUserFees($transaction, $feeIds) == 0) {
45
            return $this->redirectToRoute('index');
46
        }
47
48
        $entityManager->persist($transaction);
49
        $entityManager->flush();
50
51
        return $this->render('views/pay.html.twig', [
52
            'user_id' => $transaction->getUserId(),
53
            'invoice_number' => $transaction->getInvoiceNumber(),
54
            'total_balance' => $transaction->getTotalBalance(),
55
            'payflow_url' => getEnv("PAYFLOW_URL"),
56
            'payflow_login' => getEnv("PAYFLOW_LOGIN"),
57
            'payflow_partner' => getEnv("PAYFLOW_PARTNER"),
58
        ]);
59
    }
60
61
    /**
62
     * Use the fee id to get the information about the fee (including balance) from Alma, than add them to the transaction.
63
     * @param Transaction $transaction
64
     * @param $feeIds
65
     * @return float
66
     * @throws \GuzzleHttp\Exception\GuzzleException
67
     */
68
    private function setUserFees(Transaction $transaction, $feeIds)
69
    {
70
        $userId = $transaction->getUserId();
71
        $almaFees = $this->userData->listFees($this->api->getUserFees($userId));
72
73
        $total = 0.0;
74
        foreach ($almaFees as $almaFee) {
75
            if (in_array($almaFee['id'], $feeIds)) {
76
                $fee = new Fee($almaFee['id'], $almaFee['balance'], $almaFee['label']);
77
78
                $this->getDoctrine()->getManager()->persist($fee);
79
                $transaction->addFee($fee);
80
                $total += $almaFee['balance'];
81
            }
82
        }
83
        $transaction->setTotalBalance($total);
84
85
        return $total;
86
    }
87
}
88