Passed
Push — main ( 09bd12...55dc6e )
by Iain
05:57
created

SubscriptionRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 78
rs 10
c 1
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllForCustomer() 0 3 1
A getOneActiveSubscriptionForCustomer() 0 9 2
A getActiveSubscriptionCount() 0 3 1
A getAllActiveForCustomer() 0 3 1
A hasActiveSubscription() 0 9 2
A getForPayment() 0 10 1
A updateValidUntilForAllActiveSubscriptions() 0 16 1
A getForMainAndChildExternalReference() 0 9 2
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\Athena\Repository\DoctrineCrudRepository;
18
use Parthenon\Billing\Entity\CustomerInterface;
19
use Parthenon\Billing\Entity\Payment;
20
use Parthenon\Billing\Entity\Subscription;
21
use Parthenon\Billing\Enum\SubscriptionStatus;
22
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface;
23
use Parthenon\Common\Exception\NoEntityFoundException;
24
25
class SubscriptionRepository extends DoctrineCrudRepository implements SubscriptionRepositoryInterface
26
{
27
    public function hasActiveSubscription(CustomerInterface $customer): bool
28
    {
29
        try {
30
            $this->getOneActiveSubscriptionForCustomer($customer);
31
        } catch (NoEntityFoundException $exception) {
32
            return false;
33
        }
34
35
        return true;
36
    }
37
38
    public function getOneActiveSubscriptionForCustomer(CustomerInterface $customer): Subscription
39
    {
40
        $subscription = $this->entityRepository->findOneBy(['customer' => $customer, 'active' => true]);
41
42
        if (!$subscription instanceof Subscription) {
43
            throw new NoEntityFoundException();
44
        }
45
46
        return $subscription;
47
    }
48
49
    public function getAllForCustomer(CustomerInterface $customer): array
50
    {
51
        return $this->entityRepository->findBy(['customer' => $customer]);
52
    }
53
54
    public function getAllActiveForCustomer(CustomerInterface $customer): array
55
    {
56
        return $this->entityRepository->findBy(['customer' => $customer, 'active' => true]);
57
    }
58
59
    public function updateValidUntilForAllActiveSubscriptions(CustomerInterface $customer, string $mainExternalReference, \DateTimeInterface $validUntil): void
60
    {
61
        $qb = $this->entityRepository->createQueryBuilder('s');
62
        $qb->update()
63
            ->set('s.validUntil', ':validUntil')
64
            ->set('s.updatedAt', ':now')
65
            ->where('s.customer = :customer')
66
            ->andWhere('s.status = :active')
67
            ->andWhere('s.mainExternalReference = :mainExternalReference')
68
            ->setParameter('customer', $customer)
69
            ->setParameter('mainExternalReference', $mainExternalReference)
70
            ->setParameter('validUntil', $validUntil)
71
            ->setParameter('now', new \DateTime())
72
            ->setParameter('active', SubscriptionStatus::ACTIVE);
73
        $query = $qb->getQuery();
74
        $query->execute();
75
    }
76
77
    public function getActiveSubscriptionCount(CustomerInterface $customer): int
78
    {
79
        return $this->entityRepository->count(['customer' => $customer, 'active' => true]);
80
    }
81
82
    public function getForPayment(Payment $payment): array
83
    {
84
        $qb = $this->entityRepository->createQueryBuilder('s');
85
        $qb->where(':payment MEMBER OF p.payment')
86
            ->setParameter('payment', $payment)
87
            ->orderBy('p.createdAt', 'DESC');
88
89
        $result = $qb->getQuery()->getResult();
90
91
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result 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...
92
    }
93
94
    public function getForMainAndChildExternalReference(string $mainExternalReference, string $childExternalReference): Subscription
95
    {
96
        $subscription = $this->entityRepository->findOneBy(['mainExternalReference' => $mainExternalReference, 'childExternalReference' => $childExternalReference]);
97
98
        if (!$subscription instanceof Subscription) {
99
            throw new NoEntityFoundException(sprintf("No Subscription found for main external reference '%s' and child reference '%s'", $mainExternalReference, $childExternalReference));
100
        }
101
102
        return $subscription;
103
    }
104
}
105