Passed
Branch main (b6a268)
by Iain
04:04
created

UserSubscriberSection   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildReadView() 0 11 1
A buildListView() 0 9 1
A buildEntityForm() 0 15 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
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.0.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\Subscriptions\Athena;
16
17
use Parthenon\Athena\EntityForm;
18
use Parthenon\Athena\ListView;
19
use Parthenon\Athena\ReadView;
20
use Parthenon\Subscriptions\Entity\Subscription;
21
use Parthenon\Subscriptions\Plan\Plan;
22
use Parthenon\Subscriptions\Plan\PlanManagerInterface;
23
use Parthenon\Subscriptions\Repository\SubscriberRepositoryInterface;
24
use Parthenon\User\Athena\TeamSection;
25
use Parthenon\User\Repository\UserRepositoryInterface;
26
27
class UserSubscriberSection extends TeamSection
28
{
29
    public function __construct(private SubscriberRepositoryInterface|UserRepositoryInterface $subscriberRepository, private PlanManagerInterface $planManager)
30
    {
31
        parent::__construct($this->subscriberRepository);
0 ignored issues
show
Bug introduced by
It seems like $this->subscriberRepository can also be of type Parthenon\User\Repository\UserRepositoryInterface; however, parameter $teamRepository of Parthenon\User\Athena\TeamSection::__construct() does only seem to accept Parthenon\User\Repository\TeamRepositoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        parent::__construct(/** @scrutinizer ignore-type */ $this->subscriberRepository);
Loading history...
32
    }
33
34
    public function buildListView(ListView $listView): ListView
35
    {
36
        $listView = parent::buildListView($listView);
37
        $listView->addField('identifier', 'text', link: true)
38
            ->addField('subscription.planName', 'text')
39
            ->addField('subscription.status', 'text')
40
            ->addField('subscription.validUntil', 'text');
41
42
        return $listView;
43
    }
44
45
    public function buildEntityForm(EntityForm $entityForm): EntityForm
46
    {
47
        $entityForm = parent::buildEntityForm($entityForm);
48
49
        $planNames = array_map(function (Plan $plan) {
50
            return $plan->getName();
51
        }, $this->planManager->getPlans());
52
53
        $entityForm->section('plan')
54
            ->field('subscription.planName', 'choice', ['choices' => array_combine($planNames, $planNames)])
55
            ->field('subscription.status', 'choice', ['choices' => array_combine(Subscription::STATUS_ARRAY, Subscription::STATUS_ARRAY)])
56
            ->field('subscription.validUntil', 'date')
57
            ->end();
58
59
        return $entityForm;
60
    }
61
62
    public function buildReadView(ReadView $readView): ReadView
63
    {
64
        $readView = parent::buildReadView($readView);
65
66
        $readView->section('plan')
67
            ->field('subscription.planName')
68
            ->field('subscription.validUntil')
69
            ->field('subscription.status')
70
            ->end();
71
72
        return $readView;
73
    }
74
}
75