Passed
Push — main ( 3919a2...0705b8 )
by Iain
15:39
created

getActiveSubscriptionCount()   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
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\Subscription;
20
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface;
21
use Parthenon\Common\Exception\NoEntityFoundException;
22
23
class SubscriptionRepository extends DoctrineCrudRepository implements SubscriptionRepositoryInterface
24
{
25
    public function hasActiveSubscription(CustomerInterface $customer): bool
26
    {
27
        try {
28
            $this->getOneActiveSubscriptionForCustomer($customer);
29
        } catch (NoEntityFoundException $exception) {
30
            return false;
31
        }
32
33
        return true;
34
    }
35
36
    public function getOneActiveSubscriptionForCustomer(CustomerInterface $customer): Subscription
37
    {
38
        $subscription = $this->entityRepository->findOneBy(['customer' => $customer, 'active' => true]);
39
40
        if (!$subscription instanceof Subscription) {
41
            throw new NoEntityFoundException();
42
        }
43
44
        return $subscription;
45
    }
46
47
    public function getAllForCustomer(CustomerInterface $customer): array
48
    {
49
        return $this->entityRepository->findBy(['customer' => $customer]);
50
    }
51
52
    public function getAllActiveForCustomer(CustomerInterface $customer): array
53
    {
54
        return $this->entityRepository->findBy(['customer' => $customer, 'active' => true]);
55
    }
56
57
    public function updateValidUntilForAllActiveSubscriptions(CustomerInterface $customer, string $mainExternalReference, \DateTimeInterface $validUntil): void
58
    {
59
        $qb = $this->entityRepository->createQueryBuilder('s');
60
        $qb->update()
61
            ->set('s.validUntil', ':validUntil')
62
            ->set('s.updatedAt', ':now')
63
            ->where('s.customer = :customer')
64
            ->andWhere('s.active = true')
65
            ->andWhere('s.mainExternalReference = :mainExternalReference')
66
            ->setParameter('customer', $customer)
67
            ->setParameter('mainExternalReference', $mainExternalReference)
68
            ->setParameter(':validUntil', $validUntil)
69
            ->setParameter('now', new \DateTime());
70
        $query = $qb->getQuery();
71
        $query->execute();
72
    }
73
74
    public function getActiveSubscriptionCount(CustomerInterface $customer): int
75
    {
76
        return $this->entityRepository->count(['customer' => $customer, 'active' => true]);
77
    }
78
}
79