Completed
Push — master ( 478aa0...3b6cb2 )
by Yann
02:11
created

EntryTransactionDetail::addRelatedAgents()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 6
nop 2
crap 5
1
<?php
2
3
namespace Genkgo\Camt\Decoder;
4
5
use Genkgo\Camt\DTO;
6
use \SimpleXMLElement;
7
use Genkgo\Camt\Decoder\Factory\DTO as DTOFactory;
8
use Genkgo\Camt\Iban;
9
use Money\Money;
10
use Money\Currency;
11
use Genkgo\Camt\Util\StringToUnits;
12
13
abstract class EntryTransactionDetail
14
{
15
    /**
16
     * @param DTO\EntryTransactionDetail $detail
17
     * @param SimpleXMLElement           $xmlDetail
18
     */
19 23
    public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
20 21
    {
21 23
        if (false === isset($xmlDetail->Refs)) {
22 11
            return;
23
        }
24
25 17
        $refs = $xmlDetail->Refs;
0 ignored issues
show
Bug introduced by
The property Refs does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
26 17
        $reference = new DTO\Reference();
27
28 17
        $reference->setMessageId(isset($refs->MsgId) ? (string) $refs->MsgId : null);
29 17
        $reference->setAccountServiceReference(isset($refs->AcctSvcrRef) ? (string) $refs->AcctSvcrRef : null);
30 17
        $reference->setPaymentInformationId(isset($refs->PmtInfId) ? (string) $refs->PmtInfId : null);
31 17
        $reference->setInstructionId(isset($refs->InstrId) ? (string) $refs->InstrId : null);
32 17
        $reference->setEndToEndId(isset($refs->EndToEndId) ? (string) $refs->EndToEndId : null);
33 17
        $reference->setTransactionId(isset($refs->TxId) ? (string) $refs->TxId : null);
34 17
        $reference->setMandateId(isset($refs->MndtId) ? (string) $refs->MndtId : null);
35 17
        $reference->setChequeNumber(isset($refs->ChqNb) ? (string) $refs->ChqNb : null);
36 17
        $reference->setClearingSystemReference(isset($refs->ClrSysRef) ? (string) $refs->ClrSysRef : null);
37 17
        $reference->setAccountOwnerTransactionId(isset($refs->AcctOwnrTxId) ? (string) $refs->AcctOwnrTxId : null);
38 17
        $reference->setAccountServicerTransactionId(isset($refs->AcctSvcrTxId) ? (string) $refs->AcctSvcrTxId : null);
39 18
        $reference->setMarketInfrastructureTransactionId(isset($refs->MktInfrstrctrTxId) ? (string) $refs->MktInfrstrctrTxId : null);
40 17
        $reference->setProcessingId(isset($refs->PrcgId) ? (string) $refs->PrcgId : null);
41
42 17
        foreach ($refs->Prtry as $xmlProprietary) {
43 8
            $type = isset($xmlProprietary->Tp) ? (string) $xmlProprietary->Tp : null;
44 8
            $subReference = isset($xmlProprietary->Ref) ? (string) $xmlProprietary->Ref : null;
45
46 8
            $reference->addProprietary(new DTO\ProprietaryReference($type, $subReference));
47 17
        }
48
49 17
        $detail->addReference($reference);
50 17
    }
51
52
    /**
53
     * @param DTO\EntryTransactionDetail $detail
54
     * @param SimpleXMLElement           $xmlDetail
55
     */
56 22
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
57
    {
58 22
        if (false === isset($xmlDetail->RltdPties)) {
59 1
            return;
60
        }
61
62 21
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
63 21
            if (isset($xmlRelatedParty->Cdtr)) {
64 21
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
65 21
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct;
66 21
                $relatedPartyType = $creditor = new DTO\Creditor((string) $xmlRelatedPartyType->Nm);
0 ignored issues
show
Unused Code introduced by
$creditor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
68 21
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
69 21
            }
70
71 21
            if (isset($xmlRelatedParty->Dbtr)) {
72 18
                $xmlRelatedPartyType = $xmlRelatedParty->Dbtr;
73 18
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct;
74 18
                $relatedPartyType = $debtor = new DTO\Debtor((string) $xmlRelatedPartyType->Nm);
0 ignored issues
show
Unused Code introduced by
$debtor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
76 18
                $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount);
77 18
            }
78 21
        }
79 21
    }
80
81
    /**
82
     * @param DTO\EntryTransactionDetail $detail
83
     * @param $xmlRelatedPartyType
84
     * @param $relatedPartyType
85
     * @param $xmlRelatedPartyTypeAccount
86
     * @return DTO\RelatedParty
87
     */
88 21
    protected function addRelatedParty(DTO\EntryTransactionDetail $detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount)
89
    {
90 21
        if (isset($xmlRelatedPartyType->PstlAdr)) {
91 21
            $relatedPartyType->setAddress(DTOFactory\Address::createFromXml($xmlRelatedPartyType->PstlAdr));
92 21
        }
93
94 21
        $relatedParty = new DTO\RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount));
95
96 21
        $detail->addRelatedParty($relatedParty);
97
98 21
        return $relatedParty;
99
    }
100
101
    /**
102
     * @param DTO\EntryTransactionDetail $detail
103
     * @param SimpleXMLElement           $xmlDetail
104
     */
105 20
    public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
106
    {
107 20
        if (false === isset($xmlDetail->RltdAgts)) {
108 12
            return;
109
        }
110
111 16
        foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) {
112 16
            if (isset($xmlRelatedAgent->CdtrAgt)) {
113 16
                $agent = new DTO\CreditorAgent((string)$xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->CdtrAgt->FinInstnId->BIC);
114 16
                $relatedAgent =  new DTO\RelatedAgent($agent);
115 16
                $detail->addRelatedAgent($relatedAgent);
116 16
            }
117
118 16
            if (isset($xmlRelatedAgent->DbtrAgt)) {
119 16
                $agent = new DTO\DebtorAgent((string)$xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->DbtrAgt->FinInstnId->BIC);
120 16
                $relatedAgent =  new DTO\RelatedAgent($agent);
121 16
                $detail->addRelatedAgent($relatedAgent);
122 16
            }
123 16
        }
124 16
    }
125
126
    /**
127
     * @param DTO\EntryTransactionDetail $detail
128
     * @param SimpleXMLElement           $xmlDetail
129
     */
130 23
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
131
    {
132 23
        if (false === isset($xmlDetail->RmtInf)) {
133 11
            return;
134
        }
135
136 18
        if (isset($xmlDetail->RmtInf->Ustrd)) {
137 9
            $remittanceInformation = DTO\RemittanceInformation::fromUnstructured(
138 9
                (string)$xmlDetail->RmtInf->Ustrd
139 9
            );
140 9
            $detail->setRemittanceInformation($remittanceInformation);
141
142 9
            return;
143
        }
144
145 9
        if (isset($xmlDetail->RmtInf->Strd)
146 9
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf)
147 9
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref)
148 9
        ) {
149 9
            $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured(
150 9
                (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref
151 9
            );
152 9
            $remittanceInformation = new DTO\RemittanceInformation();
153 9
            $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
154 9
            $detail->setRemittanceInformation($remittanceInformation);
155 9
        }
156 9
    }
157
158
    /**
159
     * @param DTO\EntryTransactionDetail $detail
160
     * @param SimpleXMLElement           $xmlDetail
161
     */
162 22
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
163
    {
164 22
        if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) {
165 1
            $remittanceInformation = DTO\ReturnInformation::fromUnstructured(
166 1
                (string)$xmlDetail->RtrInf->Rsn->Cd,
167 1
                (string)$xmlDetail->RtrInf->AddtlInf
168 1
            );
169 1
            $detail->setReturnInformation($remittanceInformation);
170 1
        }
171 22
    }
172
173
    /**
174
     * @param DTO\EntryTransactionDetail $detail
175
     * @param SimpleXMLElement           $xmlDetail
176
     */
177 22
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
178
    {
179 22
        if (isset($xmlDetail->AddtlTxInf)) {
180 1
            $additionalInformation = new DTO\AdditionalTransactionInformation(
181 1
                (string) $xmlDetail->AddtlTxInf
182 1
            );
183 1
            $detail->setAdditionalTransactionInformation($additionalInformation);
184 1
        }
185 22
    }
186
187
    /**
188
     * @param DTO\EntryTransactionDetail $detail
189
     * @param SimpleXMLElement           $xmlDetail
190
     */
191 20
    public function addBankTransactionCode(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
192
    {
193 20
        $bankTransactionCode = new DTO\BankTransactionCode();
194
195 20
        if (isset($xmlDetail->BkTxCd)) {
196 18
            $bankTransactionCode = new DTO\BankTransactionCode();
197
198 18
            if (isset($xmlDetail->BkTxCd->Prtry)) {
199 18
                $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
200 18
                    (string)$xmlDetail->BkTxCd->Prtry->Cd,
201 18
                    (string)$xmlDetail->BkTxCd->Prtry->Issr
202 18
                );
203
204 18
                $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
205 18
            }
206 18
        }
207
208 20
        $detail->setBankTransactionCode($bankTransactionCode);
209 20
    }
210
211
    /**
212
     * @param DTO\EntryTransactionDetail $detail
213
     * @param SimpleXMLElement           $xmlDetail
214
     * @param SimpleXMLElement           $CdtDbtInd
215
     */
216 20
    public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd)
217
    {
218 20
        if (isset($xmlDetail->AmtDtls)) {
219 6
            $amountDetails = new DTO\AmountDetails();
220
221 6
            if (isset($xmlDetail->AmtDtls->TxAmt) && isset($xmlDetail->AmtDtls->TxAmt->Amt)) {
222 6
                $amount = StringToUnits::convert((string) $xmlDetail->AmtDtls->TxAmt->Amt);
223
                
224 6
                if ((string) $CdtDbtInd === 'DBIT') {
225 6
                    $amount = $amount * -1;
226 6
                }
227
228 6
                $money = new Money(
229 6
                    $amount,
230 6
                    new Currency((string) $xmlDetail->AmtDtls->TxAmt->Amt['Ccy'])
231 6
                );
232 6
                $amountDetails->setAmount($money);
233 6
            }
234 6
            $detail->setAmountDetails($amountDetails);
235 6
        }
236 20
    }
237
238
    /**
239
     * @param SimpleXMLElement $xmlDetail
0 ignored issues
show
Bug introduced by
There is no parameter named $xmlDetail. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
240
     *
241
     * @return DTO\Account|null
242
     */
243
    abstract public function getRelatedPartyAccount(SimpleXMLElement $xmlRelatedPartyTypeAccount);
244
}
245