Passed
Push — main ( 913bab...cf852c )
by Iain
04:08
created

ProductSection::preSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
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\Athena;
16
17
use Parthenon\Athena\AbstractSection;
18
use Parthenon\Athena\EntityForm;
19
use Parthenon\Athena\Repository\CrudRepositoryInterface;
20
use Parthenon\Billing\Entity\Product;
21
use Parthenon\Billing\Obol\ProductRegisterInterface;
22
use Parthenon\Billing\Repository\ProductRepositoryInterface;
23
24
class ProductSection extends AbstractSection
25
{
26
    public function __construct(
27
        private ProductRepositoryInterface $productRepository,
28
        private ProductRegisterInterface $productRegister,
29
    ) {
30
    }
31
32
    public function getUrlTag(): string
33
    {
34
        return 'billing-product';
35
    }
36
37
    public function getRepository(): CrudRepositoryInterface
38
    {
39
        return $this->productRepository;
40
    }
41
42
    public function getEntity()
43
    {
44
        return new Product();
45
    }
46
47
    public function getMenuSection(): string
48
    {
49
        return 'Billing';
50
    }
51
52
    public function getMenuName(): string
53
    {
54
        return 'Product';
55
    }
56
57
    /**
58
     * @param Product $entity
59
     */
60
    public function preSave($entity): void
61
    {
62
        if (!$entity->hasExternalReference()) {
63
            $this->productRegister->registerProduct($entity);
64
        }
65
    }
66
67
    public function buildEntityForm(EntityForm $entityForm): EntityForm
68
    {
69
        $entityForm->section('Main')
70
            ->field('name', 'text')
71
            ->field('external_reference', 'text', ['required' => false])
72
            ->end();
73
74
        return $entityForm;
75
    }
76
}
77