Passed
Push — main ( a58268...9c0f97 )
by Iain
04:35
created

PriceSection::preSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 11
rs 9.9666
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\Athena;
16
17
use Obol\Model\CreatePrice;
18
use Obol\Provider\ProviderInterface;
19
use Parthenon\Athena\AbstractSection;
20
use Parthenon\Athena\EntityForm;
21
use Parthenon\Athena\ListView;
22
use Parthenon\Athena\Repository\CrudRepositoryInterface;
23
use Parthenon\Billing\Entity\Price;
24
use Parthenon\Billing\Repository\PriceRepositoryInterface;
25
26
class PriceSection extends AbstractSection
27
{
28
    public function __construct(
29
        private PriceRepositoryInterface $priceRepository,
30
        private ProviderInterface $provider,
31
    ) {
32
    }
33
34
    public function getUrlTag(): string
35
    {
36
        return 'billing-price';
37
    }
38
39
    public function getRepository(): CrudRepositoryInterface
40
    {
41
        return $this->priceRepository;
42
    }
43
44
    public function getEntity()
45
    {
46
        return new Price();
47
    }
48
49
    public function getMenuSection(): string
50
    {
51
        return 'Billing';
52
    }
53
54
    public function getMenuName(): string
55
    {
56
        return 'Prices';
57
    }
58
59
    /**
60
     * @param Price $entity
61
     */
62
    public function preSave($entity): void
63
    {
64
        if (!$entity->hasExternalReference()) {
65
            $createPrice = new CreatePrice();
66
            $createPrice->setMoney($entity->getAsMoney());
67
            $createPrice->setIncludingTax($entity->isIncludingTax());
68
            $createPrice->setPaymentSchedule($entity->getSchedule());
69
            $createPrice->setRecurring($entity->isRecurring());
70
            $createPrice->setProductReference('prod_K41oMALa5jMGjp');
71
            $creation = $this->provider->prices()->createPrice($createPrice);
72
            $entity->setExternalReference($creation->getReference());
73
        }
74
    }
75
76
    public function buildListView(ListView $listView): ListView
77
    {
78
        $listView->addField('amount', 'text')
79
            ->addField('currency', 'text')
80
            ->addField('schedule', 'text');
81
82
        return $listView;
83
    }
84
85
    public function buildEntityForm(EntityForm $entityForm): EntityForm
86
    {
87
        $entityForm->section('Main')
88
                ->field('amount')
89
                ->field('currency', 'choice', ['choices' => ['Euro' => 'EUR', 'British Pounds' => 'GBP', 'US Dollars' => 'USD', 'AU Dollars' => 'AUD']])
90
                ->field('recurring', 'checkbox', ['required' => false])
91
                ->field('schedule', 'choice', ['choices' => ['Yearly' => 'year', 'Monthly' => 'month', 'Weekly' => 'week']])
92
                ->field('includingTax', 'checkbox', ['required' => false])
93
            ->end();
94
95
        return $entityForm;
96
    }
97
}
98