Passed
Push — main ( 2bc2c3...01361c )
by Iain
04:31
created

getPaymentMethodForCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Iain Cambridge 2020-2023.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.2.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Billing\Repository\Orm;
16
17
use Parthenon\Billing\Entity\CustomerInterface;
18
use Parthenon\Billing\Entity\PaymentMethod;
19
use Parthenon\Billing\Repository\PaymentMethodRepositoryInterface;
20
use Parthenon\Common\Exception\NoEntityFoundException;
21
use Parthenon\Common\Repository\DoctrineRepository;
22
23
final class PaymentMethodRepository extends DoctrineRepository implements PaymentMethodRepositoryInterface
24
{
25
    public function getPaymentMethodForCustomer(CustomerInterface $customer): array
26
    {
27
        $qb = $this->entityRepository->createQueryBuilder('pd');
28
        $qb->where('pd.deleted = false')
29
            ->andWhere('pd.customer = :customer')
30
            ->setParameter('customer', $customer);
31
        $query = $qb->getQuery();
32
        $query->execute();
33
34
        return $query->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
35
    }
36
37
    public function markAllCustomerMethodsAsNotDefault(CustomerInterface $customer): void
38
    {
39
        $qb = $this->entityRepository->createQueryBuilder('pd');
40
        $qb->update(PaymentMethod::class, 'pd')
41
            ->set('pd.defaultPaymentOption', 'false')
42
            ->where('pd.customer = :customer')
43
            ->setParameter(':customer', $customer);
44
        $qb->getQuery()->execute();
45
    }
46
47
    public function getDefaultPaymentMethodForCustomer(CustomerInterface $customer): PaymentMethod
48
    {
49
        $qb = $this->entityRepository->createQueryBuilder('pd');
50
        $qb->where('pd.deleted = false')
51
            ->andWhere('pd.defaultPaymentOption = true')
52
            ->andWhere('pd.customer = :customer')
53
            ->setParameter('customer', $customer);
54
        $query = $qb->getQuery();
55
        $query->execute();
56
57
        $paymentDetails = $query->getOneOrNullResult();
58
59
        if (!$paymentDetails instanceof PaymentMethod) {
60
            throw new NoEntityFoundException();
61
        }
62
63
        return $paymentDetails;
64
    }
65
66
    public function getPaymentMethodForCustomerAndReference(CustomerInterface $customer, string $reference): PaymentMethod
67
    {
68
        $qb = $this->entityRepository->createQueryBuilder('pd');
69
        $qb->where('pd.deleted = false')
70
            ->andWhere('pd.customer = :customer')
71
            ->andWhere('pd.storedPaymentReference = :reference')
72
            ->setParameter('customer', $customer)
73
            ->setParameter('reference', $reference);
74
        $query = $qb->getQuery();
75
        $query->execute();
76
77
        return $query->getResult()[0] ?? throw new NoEntityFoundException();
78
    }
79
80
    public function getPaymentMethodForReference(string $reference): PaymentMethod
81
    {
82
        $qb = $this->entityRepository->createQueryBuilder('pd');
83
        $qb->where('pd.deleted = false')
84
            ->andWhere('pd.storedPaymentReference = :reference')
85
            ->setParameter('reference', $reference);
86
        $query = $qb->getQuery();
87
        $query->execute();
88
89
        return $query->getResult()[0] ?? throw new NoEntityFoundException();
90
    }
91
}
92