Completed
Pull Request — master (#52)
by Rafał
04:38
created

Subscription::setMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Component\Core\Model;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use PH\Component\Core\SubscriptionPurchaseStates;
10
use PH\Component\Core\SubscriptionPaymentStates;
11
use PH\Component\Subscription\Model\Subscription as BaseSubscription;
12
13
class Subscription extends BaseSubscription implements SubscriptionInterface
14
{
15
    /**
16
     * @var Collection|PaymentInterface[]
17
     */
18
    protected $payments;
19
20
    /**
21
     * @var string
22
     */
23
    protected $purchaseState = SubscriptionPurchaseStates::STATE_NEW;
24
25
    /**
26
     * @var string
27
     */
28
    protected $paymentState = SubscriptionPaymentStates::STATE_NEW;
29
30
    /**
31
     * @var null|string
32
     */
33
    protected $tokenValue;
34
35
    /**
36
     * @var PaymentMethodInterface
37
     */
38
    protected $method;
39
40
    /**
41
     * Subscription constructor.
42
     */
43
    public function __construct()
44
    {
45
        parent::__construct();
46
47
        $this->payments = new ArrayCollection();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getPayments(): Collection
54
    {
55
        return $this->payments;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function setPayments(Collection $payments): void
62
    {
63
        $this->payments = $payments;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function hasPayments(): bool
70
    {
71
        return !$this->payments->isEmpty();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function addPayment(PaymentInterface $payment): void
78
    {
79
        /** @var PaymentInterface $payment */
80
        if (!$this->hasPayment($payment)) {
81
            $this->payments->add($payment);
82
            $payment->setSubscription($this);
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function removePayment(PaymentInterface $payment): void
90
    {
91
        /** @var PaymentInterface $payment */
92
        if ($this->hasPayment($payment)) {
93
            $this->payments->removeElement($payment);
94
            $payment->setSubscription(null);
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function hasPayment(PaymentInterface $payment): bool
102
    {
103
        return $this->payments->contains($payment);
104
    }
105
106
    /**
107
     * @param string|null $state
108
     *
109
     * @return PaymentInterface|null
110
     */
111
    public function getLastPayment(string $state = null): ?PaymentInterface
112
    {
113
        if ($this->payments->isEmpty()) {
114
            return null;
115
        }
116
117
        $payment = $this->payments->filter(function (PaymentInterface $payment) use ($state) {
118
            return null === $state || $payment->getState() === $state;
119
        })->last();
120
121
        return false !== $payment ? $payment : null;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getPurchaseState(): string
128
    {
129
        return $this->purchaseState;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function setPurchaseState(string $purchaseState): void
136
    {
137
        $this->purchaseState = $purchaseState;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getPaymentState(): string
144
    {
145
        return $this->paymentState;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function setPaymentState(string $paymentState): void
152
    {
153
        $this->paymentState = $paymentState;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getTokenValue(): ?string
160
    {
161
        return $this->tokenValue;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function setTokenValue(string $tokenValue): void
168
    {
169
        $this->tokenValue = $tokenValue;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getMethod(): ?PaymentMethodInterface
176
    {
177
        return $this->method;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function setMethod(?PaymentMethodInterface $method): void
184
    {
185
        $this->method = $method;
186
    }
187
}
188