Passed
Push — master ( 9536d0...0dc3be )
by Jan
12:11
created

SEPAExport::getXMLContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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

330
        $data = SEPAXMLParser::parseFromFile(/** @scrutinizer ignore-type */ $this->xml_file);
Loading history...
331
332
        $this->sepa_message_id = $data['msg_id'];
333
        $this->number_of_payments = $data['number_of_payments'];
334
        $this->total_sum = $data['total_sum'];
335
        $this->initiator_bic = $data['initiator_bic'];
336
        $this->initiator_iban = $data['initiator_iban'];
337
    }
338
339
    /**
340
     * Creates a new SEPAExport with the content of the given XML string.
341
     * A new temporary file is created with the content which is later saved in the proper folder during persisting to database.
342
     * @param  string  $xml_string The content of the the XML file
343
     * @param  string $original_filename The value which should be used in database entry original filename.
344
     * @return SEPAExport
345
     */
346
    public static function createFromXMLString(string $xml_string, string $original_filename): SEPAExport
347
    {
348
        $tmpfname = tempnam(sys_get_temp_dir(), 'stura_') . '.xml';
349
        file_put_contents($tmpfname, $xml_string);
350
351
        $export = new SEPAExport();
352
        $export->setXmlFile(new UploadedFile($tmpfname, $original_filename, 'application/xml'));
353
354
        return $export;
355
    }
356
357
}