Passed
Push — master ( 2b7bb3...0db2dd )
by Clint A
06:52
created

PayController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
class PayController extends Controller
14
{
15
16
    public function __construct(AlmaApi $api)
17
    {
18
        $this->almaApi = $api;
0 ignored issues
show
Bug Best Practice introduced by
The property almaApi does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
    }
20
21
    /**
22
     * @Route("/pay", name="payment_handler")
23
     * @param Request $request
24
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
25
     * @throws \GuzzleHttp\Exception\GuzzleException
26
     */
27
    public function index(Request $request)
28
    {
29
        $feeIds = $request->request->get('fee');
30
        if (!isset($feeIds) || !count($feeIds)) {
31
            return $this->redirectToRoute('index');
32
        }
33
34
        $user_id = $request->request->get('user_id');
35
        $transaction = new Transaction($user_id);
36
37
        $entityManager = $this->getDoctrine()->getManager();
38
        $this->setUserFees($transaction, $feeIds);
39
40
        $entityManager->persist($transaction);
41
        $entityManager->flush();
42
43
        return $this->render('views/pay.html.twig', [
44
            'user_id' => $transaction->getUserId(),
45
            'invoice_number' => $transaction->getInvoiceNumber(),
46
            'total_balance' => $transaction->getTotalBalance(),
47
            'payflow_url' => getEnv("PAYFLOW_URL"),
48
            'payflow_login' => getEnv("PAYFLOW_LOGIN"),
49
            'payflow_partner' => getEnv("PAYFLOW_PARTNER"),
50
        ]);
51
    }
52
53
    /**
54
     * Use the fee id to get the information about the fee (including balance) from Alma, than add them to the transaction.
55
     * @param Transaction $transaction
56
     * @param $feeIds
57
     * @throws \GuzzleHttp\Exception\GuzzleException
58
     */
59
    private function setUserFees(Transaction $transaction, $feeIds)
60
    {
61
        $userData = new AlmaUserData();
62
63
        $userId = $transaction->getUserId();
64
        $almaFees = $userData->listFees($this->almaApi->getUserFees($userId));
65
66
        $total = 0;
67
        foreach ($almaFees as $almaFee) {
68
            if (in_array($almaFee['id'], $feeIds)) {
69
                $fee = new Fee($almaFee['id'], $almaFee['balance'], $almaFee['label']);
70
71
                $this->getDoctrine()->getManager()->persist($fee);
72
                $transaction->addFee($fee);
73
                $total += $almaFee['balance'];
74
            }
75
        }
76
        $transaction->setTotalBalance($total);
77
    }
78
}
79