Passed
Push — master ( a707ad...480443 )
by Jan
05:15
created

SEPAExport::setGroupUlid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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\HttpFoundation\File\UploadedFile;
28
use Symfony\Component\Uid\Ulid;
29
use Symfony\Component\Validator\Constraints as Assert;
30
use Vich\UploaderBundle\Entity\File;
31
use Vich\UploaderBundle\Mapping\Annotation as Vich;
32
33
/**
34
 * @ORM\Entity()
35
 * @ORM\Table("sepa_exports")
36
 * @Vich\Uploadable()
37
 * @ORM\HasLifecycleCallbacks()
38
 */
39
class SEPAExport implements DBElementInterface, TimestampedElementInterface
40
{
41
42
    use TimestampTrait;
43
44
    /**
45
     * @ORM\Id()
46
     * @ORM\GeneratedValue()
47
     * @ORM\Column(type="integer")
48
     */
49
    private $id;
50
51
    /**
52
     * @var int|null The number of payments contained in this export
53
     * @ORM\Column(type="integer")
54
     */
55
    private $number_of_payments = null;
56
57
    /**
58
     * @var int|null The total sum of payments in this payment (in cents)
59
     * @ORM\Column(type="integer")
60
     */
61
    private $total_sum = null;
62
63
    /**
64
     * @var Collection|PaymentOrder[]
65
     * @ORM\ManyToMany(targetEntity="App\Entity\PaymentOrder")
66
     */
67
    private $associated_payment_orders;
68
69
    /**
70
     * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
71
     *
72
     * @var File
73
     */
74
    private $xml;
75
76
    /**
77
     * @Vich\UploadableField(mapping="sepa_export_xml", fileNameProperty="references.name", size="references.size", mimeType="references.mimeType", originalName="references.originalName", dimensions="references.dimensions")
78
     *
79
     * @var \Symfony\Component\HttpFoundation\File\File|null
80
     * @Assert\NotBlank(groups={"frontend"})
81
     * @Assert\File(
82
     *     maxSize = "10M",
83
     *     mimeTypes = {"application/pdf", "application/x-pdf"},
84
     *     mimeTypesMessage = "validator.upload_pdf"
85
     * )
86
     */
87
    private $xml_file;
88
89
    /**
90
     * @var string|null The SEPA message id of the assiociated file.
91
     * @ORM\Column(type="text")
92
     */
93
    private $sepa_message_id = null;
94
95
    /**
96
     * @var string|null The BIC of the debtor
97
     * @ORM\Column(type="text")
98
     */
99
    private $initiator_bic = null;
100
101
    /**
102
     * @var \DateTime|null The datetime when this SEPA export was booked (or marked as booked) in banking. Null if it was not booked yet.
103
     * @ORM\Column(type="datetime")
104
     */
105
    private $booking_date = null;
106
107
    /**
108
     * @var string A text describing this SEPA export
109
     * @ORM\Column(type="text")
110
     */
111
    private $description = "";
112
113
    /**
114
     * @var string A comment for this SEPA export
115
     * @ORM\Column(type="text")
116
     */
117
    private $comment = "";
118
119
    /**
120
     * @var string|null The IBAN of the debtor
121
     * @ORM\Column(type="text")
122
     */
123
    private $initiator_iban = null;
124
125
    /**
126
     * The export group this SEPA export belongs to. Null if it belongs to no export group.
127
     * @ORM\Column(type="ulid", nullable=true)
128
     * @var null|Ulid
129
     */
130
    private $group_ulid = null;
131
132
    public function __construct()
133
    {
134
        $this->associated_payment_orders = new ArrayCollection();
135
        $this->xml = new File();
136
137
        $this->creation_date = new \DateTime();
138
        $this->last_modified = new \DateTime();
139
    }
140
141
    public function getId(): ?int
142
    {
143
        return $this->id;
144
    }
145
146
    /**
147
     * Returns the number of payments in this SEPA export
148
     * @return int
149
     */
150
    public function getNumberOfPayments(): ?int
151
    {
152
        return $this->number_of_payments;
153
    }
154
155
    /**
156
     * Returns the total sum of all payments in this SEPA export in cents
157
     * @return int
158
     */
159
    public function getTotalSum(): ?int
160
    {
161
        return $this->total_sum;
162
    }
163
164
    /**
165
     * Returns the payment orders which are associated to this SEPA export
166
     * @return PaymentOrder[]|Collection
167
     */
168
    public function getAssociatedPaymentOrders()
169
    {
170
        return $this->associated_payment_orders;
171
    }
172
173
    /**
174
     * Returns the associated XML file
175
     * @return File
176
     */
177
    public function getXml(): File
178
    {
179
        return $this->xml;
180
    }
181
182
    /**
183
     * Returns the associated XML file
184
     * @return \Symfony\Component\HttpFoundation\File\File|null
185
     */
186
    public function getXmlFile(): ?\Symfony\Component\HttpFoundation\File\File
187
    {
188
        return $this->xml_file;
189
    }
190
191
    /**
192
     * Returns the content of the associated XML file.
193
     * @throws \RuntimeException if no xml file was set yet.
194
     * @return string
195
     */
196
    public function getXMLContent(): string
197
    {
198
        if ($this->getXmlFile() === null) {
199
            throw new \RuntimeException("No XML file provided yet!");
200
        }
201
202
        return $this->xml_file->getContent();
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on null. ( Ignorable by Annotation )

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

202
        return $this->xml_file->/** @scrutinizer ignore-call */ getContent();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
    }
204
205
    /**
206
     * Returns the sepa message id associated with this file
207
     * @return string
208
     */
209
    public function getSepaMessageId(): ?string
210
    {
211
        return $this->sepa_message_id;
212
    }
213
214
    /**
215
     * Returns the BIC of the debitor
216
     * @return string
217
     */
218
    public function getInitiatorBic(): ?string
219
    {
220
        return $this->initiator_bic;
221
    }
222
223
    /**
224
     * Returns the IBAN of the debitor
225
     * @return string
226
     */
227
    public function getInitiatorIban(): ?string
228
    {
229
        return $this->initiator_iban;
230
    }
231
232
    /**
233
     * Returns the datetime, when this export was marked as booked. Null if it was not booked yet.
234
     * @return \DateTime|null
235
     */
236
    public function getBookingDate(): ?\DateTime
237
    {
238
        return $this->booking_date;
239
    }
240
241
    /**
242
     * Returns true, if this export was not booked yet.
243
     * @return bool
244
     */
245
    public function isOpen(): bool
246
    {
247
        return ($this->booking_date === null);
248
    }
249
250
    /**
251
     * Returns a short describtion for this export
252
     * @return string
253
     */
254
    public function getDescription(): string
255
    {
256
        return $this->description;
257
    }
258
259
    /**
260
     * Returns a comment for this export
261
     * @return string
262
     */
263
    public function getComment(): string
264
    {
265
        return $this->comment;
266
    }
267
268
    /**
269
     * Sets the booking status. If set to true, the booking date is set to now.
270
     * @param  bool  $is_booked
271
     * @return $this
272
     */
273
    public function setIsBooked(bool $is_booked = true): self
274
    {
275
        if ($is_booked === false) {
276
            //Reset booking state
277
            $this->booking_date = null;
278
        } else {
279
            //Set booking date to now
280
            $this->booking_date = new \DateTime();
281
        }
282
283
        return $this;
284
    }
285
286
    /**
287
     * @param  File  $xml
288
     * @return SEPAExport
289
     */
290
    public function setXml(File $xml): SEPAExport
291
    {
292
        $this->xml = $xml;
293
        return $this;
294
    }
295
296
    /**
297
     * @param  \Symfony\Component\HttpFoundation\File\File  $xml_file
298
     * @return SEPAExport
299
     */
300
    public function setXmlFile(\Symfony\Component\HttpFoundation\File\File $xml_file): SEPAExport
301
    {
302
        //Check if $xml_file was changed
303
        if($this->xml_file === null || $xml_file->getPathname() !== $this->xml_file->getPathname()) {
304
            $this->xml_file = $xml_file;
305
            $this->updateFromFile();
306
        } else {
307
            $this->xml_file = $xml_file;
308
        }
309
        return $this;
310
    }
311
312
    /**
313
     * @param  string  $description
314
     * @return SEPAExport
315
     */
316
    public function setDescription(string $description): SEPAExport
317
    {
318
        $this->description = $description;
319
        return $this;
320
    }
321
322
    /**
323
     * @param  string  $comment
324
     * @return SEPAExport
325
     */
326
    public function setComment(string $comment): SEPAExport
327
    {
328
        $this->comment = $comment;
329
        return $this;
330
    }
331
332
    /**
333
     * Update the total sum, number of payments, message id, debitor iban and bic from the associatated XML file
334
     * @return void
335
     */
336
    public function updateFromFile(): void
337
    {
338
        $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

338
        $data = SEPAXMLParser::parseFromFile(/** @scrutinizer ignore-type */ $this->xml_file);
Loading history...
339
340
        $this->sepa_message_id = $data['msg_id'];
341
        $this->number_of_payments = $data['number_of_payments'];
342
        $this->total_sum = $data['total_sum'];
343
        $this->initiator_bic = $data['initiator_bic'];
344
        $this->initiator_iban = $data['initiator_iban'];
345
    }
346
347
    /**
348
     * Creates a new SEPAExport with the content of the given XML string.
349
     * A new temporary file is created with the content which is later saved in the proper folder during persisting to database.
350
     * @param  string  $xml_string The content of the the XML file
351
     * @param  string $original_filename The value which should be used in database entry original filename.
352
     * @return SEPAExport
353
     */
354
    public static function createFromXMLString(string $xml_string, string $original_filename): SEPAExport
355
    {
356
        $tmpfname = tempnam(sys_get_temp_dir(), 'stura_') . '.xml';
357
        file_put_contents($tmpfname, $xml_string);
358
359
        $export = new SEPAExport();
360
        $export->setXmlFile(new UploadedFile($tmpfname, $original_filename, 'application/xml'));
361
362
        return $export;
363
    }
364
365
    /**
366
     * Returns the ulid of the export group this export belongs to. Null if this export belongs to no export group.
367
     * @return Ulid|null
368
     */
369
    public function getGroupUlid(): ?Ulid
370
    {
371
        return $this->group_ulid;
372
    }
373
374
    /**
375
     * Sets the ulid of the export group this export belongs to. Set to null if this export belongs to no export group.
376
     * @param  Ulid|null  $group_ulid
377
     * @return SEPAExport
378
     */
379
    public function setGroupUlid(?Ulid $group_ulid): SEPAExport
380
    {
381
        $this->group_ulid = $group_ulid;
382
        return $this;
383
    }
384
385
386
387
}