Passed
Push — master ( 98831a...e4d06f )
by Jan
04:14
created

SEPAExport   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 285
rs 10
wmc 23

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setDescription() 0 4 1
A getNumberOfPayments() 0 3 1
A getComment() 0 3 1
A getSepaMessageId() 0 3 1
A setXmlFile() 0 10 3
A __construct() 0 7 1
A getAssociatedPaymentOrders() 0 3 1
A getInitiatorBic() 0 3 1
A setComment() 0 4 1
A setIsBooked() 0 11 2
A getId() 0 3 1
A setXml() 0 4 1
A getBookingDate() 0 3 1
A updateFromFile() 0 9 1
A getXml() 0 3 1
A getDescription() 0 3 1
A getXmlFile() 0 3 1
A getInitiatorIban() 0 3 1
A getTotalSum() 0 3 1
A isOpen() 0 3 1
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Entity;
20
21
use App\Entity\Contracts\DBElementInterface;
22
use App\Entity\Contracts\TimestampedElementInterface;
23
use App\Helpers\SEPAXML\SEPAXMLParser;
24
use Doctrine\Common\Collections\ArrayCollection;
25
use Doctrine\Common\Collections\Collection;
26
use Doctrine\ORM\Mapping as ORM;
27
use Symfony\Component\Validator\Constraints as Assert;
28
use Vich\UploaderBundle\Entity\File;
29
use Vich\UploaderBundle\Mapping\Annotation as Vich;
30
31
/**
32
 * @ORM\Entity()
33
 * @ORM\Table("sepa_exports")
34
 * @Vich\Uploadable()
35
 * @ORM\HasLifecycleCallbacks()
36
 */
37
class SEPAExport implements DBElementInterface, TimestampedElementInterface
38
{
39
40
    use TimestampTrait;
41
42
    /**
43
     * @ORM\Id()
44
     * @ORM\GeneratedValue()
45
     * @ORM\Column(type="integer")
46
     */
47
    private $id;
48
49
    /**
50
     * @var int|null The number of payments contained in this export
51
     * @ORM\Column(type="integer")
52
     */
53
    private $number_of_payments = null;
54
55
    /**
56
     * @var int|null The total sum of payments in this payment (in cents)
57
     * @ORM\Column(type="integer")
58
     */
59
    private $total_sum = null;
60
61
    /**
62
     * @var Collection|PaymentOrder[]
63
     * @ORM\ManyToMany(targetEntity="App\Entity\PaymentOrder")
64
     */
65
    private $associated_payment_orders;
66
67
    /**
68
     * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
69
     *
70
     * @var File
71
     */
72
    private $xml;
73
74
    /**
75
     * @Vich\UploadableField(mapping="sepa_export_xml", fileNameProperty="references.name", size="references.size", mimeType="references.mimeType", originalName="references.originalName", dimensions="references.dimensions")
76
     *
77
     * @var \Symfony\Component\HttpFoundation\File\File|null
78
     * @Assert\NotBlank(groups={"frontend"})
79
     * @Assert\File(
80
     *     maxSize = "10M",
81
     *     mimeTypes = {"application/pdf", "application/x-pdf"},
82
     *     mimeTypesMessage = "validator.upload_pdf"
83
     * )
84
     */
85
    private $xml_file;
86
87
    /**
88
     * @var string|null The SEPA message id of the assiociated file.
89
     * @ORM\Column(type="text")
90
     */
91
    private $sepa_message_id = null;
92
93
    /**
94
     * @var string|null The BIC of the debtor
95
     * @ORM\Column(type="text")
96
     */
97
    private $initiator_bic = null;
98
99
    /**
100
     * @var \DateTime|null The datetime when this SEPA export was booked (or marked as booked) in banking. Null if it was not booked yet.
101
     * @ORM\Column(type="datetime")
102
     */
103
    private $booking_date = null;
104
105
    /**
106
     * @var string A text describing this SEPA export
107
     * @ORM\Column(type="text")
108
     */
109
    private $description = "";
110
111
    /**
112
     * @var string A comment for this SEPA export
113
     * @ORM\Column(type="text")
114
     */
115
    private $comment = "";
116
117
    /**
118
     * @var string|null The IBAN of the debtor
119
     * @ORM\Column(type="text")
120
     */
121
    private $initiator_iban = null;
122
123
    public function __construct()
124
    {
125
        $this->associated_payment_orders = new ArrayCollection();
126
        $this->xml = new File();
127
128
        $this->creation_date = new \DateTime();
129
        $this->last_modified = new \DateTime();
130
    }
131
132
    public function getId(): ?int
133
    {
134
        return $this->id;
135
    }
136
137
    /**
138
     * Returns the number of payments in this SEPA export
139
     * @return int
140
     */
141
    public function getNumberOfPayments(): ?int
142
    {
143
        return $this->number_of_payments;
144
    }
145
146
    /**
147
     * Returns the total sum of all payments in this SEPA export in cents
148
     * @return int
149
     */
150
    public function getTotalSum(): ?int
151
    {
152
        return $this->total_sum;
153
    }
154
155
    /**
156
     * Returns the payment orders which are associated to this SEPA export
157
     * @return PaymentOrder[]|Collection
158
     */
159
    public function getAssociatedPaymentOrders()
160
    {
161
        return $this->associated_payment_orders;
162
    }
163
164
    /**
165
     * Returns the associated XML file
166
     * @return File
167
     */
168
    public function getXml(): File
169
    {
170
        return $this->xml;
171
    }
172
173
    /**
174
     * Returns the associated XML file
175
     * @return \Symfony\Component\HttpFoundation\File\File|null
176
     */
177
    public function getXmlFile(): ?\Symfony\Component\HttpFoundation\File\File
178
    {
179
        return $this->xml_file;
180
    }
181
182
    /**
183
     * Returns the sepa message id associated with this file
184
     * @return string
185
     */
186
    public function getSepaMessageId(): ?string
187
    {
188
        return $this->sepa_message_id;
189
    }
190
191
    /**
192
     * Returns the BIC of the debitor
193
     * @return string
194
     */
195
    public function getInitiatorBic(): ?string
196
    {
197
        return $this->initiator_bic;
198
    }
199
200
    /**
201
     * Returns the IBAN of the debitor
202
     * @return string
203
     */
204
    public function getInitiatorIban(): ?string
205
    {
206
        return $this->initiator_iban;
207
    }
208
209
    /**
210
     * Returns the datetime, when this export was marked as booked. Null if it was not booked yet.
211
     * @return \DateTime|null
212
     */
213
    public function getBookingDate(): ?\DateTime
214
    {
215
        return $this->booking_date;
216
    }
217
218
    /**
219
     * Returns true, if this export was not booked yet.
220
     * @return bool
221
     */
222
    public function isOpen(): bool
223
    {
224
        return ($this->booking_date === null);
225
    }
226
227
    /**
228
     * Returns a short describtion for this export
229
     * @return string
230
     */
231
    public function getDescription(): string
232
    {
233
        return $this->description;
234
    }
235
236
    /**
237
     * Returns a comment for this export
238
     * @return string
239
     */
240
    public function getComment(): string
241
    {
242
        return $this->comment;
243
    }
244
245
    /**
246
     * Sets the booking status. If set to true, the booking date is set to now.
247
     * @param  bool  $is_booked
248
     * @return $this
249
     */
250
    public function setIsBooked(bool $is_booked = true): self
251
    {
252
        if ($is_booked === false) {
253
            //Reset booking state
254
            $this->booking_date = null;
255
        } else {
256
            //Set booking date to now
257
            $this->booking_date = new \DateTime();
258
        }
259
260
        return $this;
261
    }
262
263
    /**
264
     * @param  File  $xml
265
     * @return SEPAExport
266
     */
267
    public function setXml(File $xml): SEPAExport
268
    {
269
        $this->xml = $xml;
270
        return $this;
271
    }
272
273
    /**
274
     * @param  \Symfony\Component\HttpFoundation\File\File  $xml_file
275
     * @return SEPAExport
276
     */
277
    public function setXmlFile(\Symfony\Component\HttpFoundation\File\File $xml_file): SEPAExport
278
    {
279
        //Check if $xml_file was changed
280
        if($this->xml_file === null || $xml_file->getPathname() !== $this->xml_file->getPathname()) {
281
            $this->xml_file = $xml_file;
282
            $this->updateFromFile();
283
        } else {
284
            $this->xml_file = $xml_file;
285
        }
286
        return $this;
287
    }
288
289
    /**
290
     * @param  string  $description
291
     * @return SEPAExport
292
     */
293
    public function setDescription(string $description): SEPAExport
294
    {
295
        $this->description = $description;
296
        return $this;
297
    }
298
299
    /**
300
     * @param  string  $comment
301
     * @return SEPAExport
302
     */
303
    public function setComment(string $comment): SEPAExport
304
    {
305
        $this->comment = $comment;
306
        return $this;
307
    }
308
309
    /**
310
     * Update the total sum, number of payments, message id, debitor iban and bic from the associatated XML file
311
     * @return void
312
     */
313
    public function updateFromFile(): void
314
    {
315
        $data = SEPAXMLParser::parseFromFile($this->xml_file);
0 ignored issues
show
Bug introduced by
It seems like $this->xml_file can also be of type null; however, parameter $file of App\Helpers\SEPAXML\SEPAXMLParser::parseFromFile() does only seem to accept SplFileInfo, 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

315
        $data = SEPAXMLParser::parseFromFile(/** @scrutinizer ignore-type */ $this->xml_file);
Loading history...
316
317
        $this->sepa_message_id = $data['msg_id'];
318
        $this->number_of_payments = $data['number_of_payments'];
319
        $this->total_sum = $data['total_sum'];
320
        $this->initiator_bic = $data['initiator_bic'];
321
        $this->initiator_iban = $data['initiator_iban'];
322
    }
323
324
}