Inspector::bankNumberOf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace SmartCNAB\Support\File;
4
5
use RangeException;
6
7
/**
8
 * Files inspector.
9
 * Grab some util informations about files.
10
 */
11
class Inspector
12
{
13
    /**
14
     * Discover and return the bank number a file.
15
     *
16
     * @param  string  $path
17
     * @return integer
18
     */
19
    public static function bankNumberOf($path)
20
    {
21
        $file = fopen(realpath($path), 'r');
22
        $header = fgets($file);
23
        fclose($file);
24
25
        return substr($header, 76, 3);
26
    }
27
28
    /**
29
     * Discover and return a file CNAB version.
30
     *
31
     * @param  string  $path
32
     * @return integer
33
     * @throws \RangeException
34
     */
35
    public static function fileVersionOf($path)
36
    {
37
        $file = fopen(realpath($path), 'r');
38
        $header = fgets($file);
39
        fclose($file);
40
41
        $size = strlen(trim($header));
42
43
        if ( ! in_array($size, [240, 400])) {
44
            throw new RangeException('Invalid CNAB file version. Size ' . $size);
45
        }
46
47
        return $size;
48
    }
49
}
50