File   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 6
dl 0
loc 142
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A createFilename() 0 14 1
A getHeader() 0 7 1
A getFooter() 0 12 3
A getCart() 0 13 2
A getAll() 0 8 1
1
<?php
2
namespace MrPrompt\Centercob\Received;
3
4
use DateTime;
5
use MrPrompt\ShipmentCommon\Base\Cart;
6
use MrPrompt\ShipmentCommon\Base\Customer;
7
use MrPrompt\ShipmentCommon\Util\Number;
8
use MrPrompt\Centercob\Received\Partial\Detail;
9
use MrPrompt\Centercob\Received\Partial\Footer;
10
use MrPrompt\Centercob\Received\Partial\Header;
11
12
/**
13
 * Received file class
14
 *
15
 * @author Thiago Paes <[email protected]>
16
 */
17
class File
18
{
19
    /**
20
     * File name template
21
     *
22
     * @var string
23
     */
24
    const TEMPLATE_GENERATED = 'RET{CLIENT}-{DD.MM.YYYY-HH.MM.SS}.TXT';
25
26
    /**
27
     * File name template
28
     *
29
     * @var string
30
     */
31
    const TEMPLATE_PROCESSED = 'RET{CLIENT}-{DD.MM.YYYY-HH.MM.SS}.TXT';
32
33
    /**
34
     * @var Header
35
     */
36
    private $header;
37
38
    /**
39
     * @var ArrayObject
40
     */
41
    private $cart;
42
43
    /**
44
     * @var Footer
45
     */
46
    private $footer;
47
48
    /**
49
     * @var array
50
     */
51
    private $rows = [];
52
53
    /**
54
     * @var Customer
55
     */
56
    private $customer;
57
58
    /**
59
     * @var DateTime
60
     */
61
    private $now;
62
63
    /**
64
     * @param Customer $customer
65
     * @param DateTime $date
66
     * @param string $storageDir
67
     */
68 1
    public function __construct(Customer $customer, DateTime $date, $storageDir = null)
69
    {
70 1
        $this->customer = $customer;
71 1
        $this->now      = $date;
72
73 1
        $file           = $storageDir . DIRECTORY_SEPARATOR . $this->createFilename();
74 1
        $content        =  file_get_contents($file);
75
76 1
        $this->rows     = explode(PHP_EOL, $content);
77
78 1
        $this->getHeader();
79 1
        $this->getFooter();
80 1
        $this->getCart();
81 1
    }
82
83
    /**
84
     * Create the file name
85
     *
86
     * @return string
87
     */
88 1
    private function createFilename($type = self::TEMPLATE_GENERATED)
89
    {
90
        $search = [
91 1
            '{CLIENT}',
92
            '{DD.MM.YYYY-HH.MM.SS}',
93
        ];
94
95
        $replace = [
96 1
            Number::zeroFill($this->customer->getCode(), 7, Number::FILL_LEFT),
97 1
            $this->now->format('d.m.Y-H.i.s'),
98
        ];
99
100 1
        return str_replace($search, $replace, $type);
101
    }
102
103
    /**
104
     * @return Header
105
     */
106 1
    public function getHeader()
107
    {
108 1
        $headerLine     = array_shift($this->rows);
109 1
        $this->header   = new Header($headerLine);
110
111 1
        return $this->header;
112
    }
113
114
    /**
115
     * @return Footer
116
     */
117 1
    public function getFooter()
118
    {
119 1
        $footerLine     = array_pop($this->rows);
120
121 1
        if (null === $footerLine || "" === $footerLine) {
122 1
            $footerLine = array_pop($this->rows);
123
        }
124
125 1
        $this->footer   = new Footer($footerLine);
126
127 1
        return $this->footer;
128
    }
129
130
    /**
131
     * @return \ArrayObject
132
     */
133 1
    public function getCart()
134
    {
135 1
        $this->cart     = new Cart();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \MrPrompt\ShipmentCommon\Base\Cart() of type object<MrPrompt\ShipmentCommon\Base\Cart> is incompatible with the declared type object<MrPrompt\Centercob\Received\ArrayObject> of property $cart.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
136
137
        /* @var $detail \Centercob\Gateway\Received\Partial\Detail */
138 1
        foreach ($this->rows as $row) {
139 1
            $detail = new Detail($row);
140
141 1
            $this->cart->addItem($detail);
142
        }
143
144 1
        return $this->cart;
145
    }
146
147
    /**
148
     * @return mixed
149
     */
150 1
    public function getAll()
151
    {
152
        return [
153 1
            $this->header,
154 1
            $this->cart,
155 1
            $this->footer
156
        ];
157
    }
158
}
159