Factory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 55
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A remittance() 0 8 1
A returning() 0 10 2
A discoverBankNamespace() 0 10 2
1
<?php
2
3
namespace SmartCNAB\Services;
4
5
use InvalidArgumentException;
6
7
use SmartCNAB\Contracts\FactoryInterface;
8
use SmartCNAB\Support\Bank;
9
use SmartCNAB\Support\File\Inspector;
10
use SmartCNAB\Support\Picture;
11
12
/**
13
 * SmartCNAB files factory
14
 */
15
class Factory implements FactoryInterface
16
{
17
    /**
18
     * Return an instance of a remittance.
19
     *
20
     * @param  integer  $bank
21
     * @param  integer  $version
22
     * @return \SmartCNAB\Contracts\File\RemittanceInterface
23
     */
24
    public function remittance($bank, $version)
25
    {
26
        $bankNs = $this->discoverBankNamespace($bank);
27
        $file = "File{$version}";
28
        $class = $bankNs . $file;
29
30
        return new $class(new Picture());
31
    }
32
33
    /**
34
     * Return an instance of a returning.
35
     *
36
     * @param  string  $path
37
     * @param  integer  $bank
38
     * @return \SmartCNAB\Contracts\File\Returning
39
     */
40
    public function returning($path, $bank = null)
41
    {
42
        $bank = $bank ?: Inspector::bankNumberOf($path);
43
        $bankNs = $this->discoverBankNamespace($bank, 'Returning');
44
        $version = Inspector::fileVersionOf($path);
45
        $file = "File{$version}";
46
        $class = $bankNs . $file;
47
48
        return new $class($path, new Picture());
49
    }
50
51
    /**
52
     * Discover and return a bank namespace with received bank code.
53
     *
54
     * @param  integer  $bank
55
     * @param  string  $type  If remittance or returning
56
     * @return string
57
     * @throws \InvalidArgumentException
58
     */
59
    protected function discoverBankNamespace($bank, $type = 'Remittances')
60
    {
61
        if ( ! Bank::isHandled($bank)) {
62
            throw new InvalidArgumentException('Unable to handle bank ' . $bank);
63
        }
64
65
        $name = Bank::nameOfNumber($bank);
66
67
        return "\SmartCNAB\Services\\{$type}\Banks\\{$name}\\";
68
    }
69
}
70