File   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 127
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getLines() 0 4 1
A load() 0 12 2
A save() 0 10 2
A classToPath() 0 6 1
A parseSchema() 0 10 2
A schemaPath() 0 4 1
A generate() 0 12 1
1
<?php
2
3
namespace SmartCNAB\Support\File;
4
5
use RuntimeException;
6
use SplFileObject;
7
8
use SmartCNAB\Contracts\File\FileInterface;
9
10
/**
11
 * Base file class.
12
 */
13
class File implements FileInterface
14
{
15
    /**
16
     * Version constants
17
     */
18
    const CNAB240 = 240;
19
    const CNAB400 = 400;
20
21
    /**
22
     * File lines collection.
23
     *
24
     * @var array
25
     */
26
    protected $lines = [];
27
28
    /**
29
     * File schema file.
30
     *
31
     * @var string
32
     */
33
    protected $schemaFile;
34
35
    /**
36
     * Return the file lines.
37
     *
38
     * @return array
39
     */
40
    public function getLines()
41
    {
42
        return $this->lines;
43
    }
44
45
    /**
46
     * Loads a file content.
47
     *
48
     * @param  string  $path
49
     * @return \SmartCNAB\Support\File\File
50
     * @throws \RuntimeException
51
     */
52
    public function load($path)
53
    {
54
        $contents = file($path);
55
56
        if ($contents === false) {
57
            throw new RuntimeException('Unable to read file ' . $path);
58
        }
59
60
        $this->lines = $contents;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Saves a file and return it.
67
     *
68
     * @param  string  $path
69
     * @return \SplFileObject
70
     * @throws \RuntimeException
71
     */
72
    public function save($path)
73
    {
74
        $output = $this->generate();
75
76
        if (file_put_contents($path, $output) === false) {
77
            throw new RuntimeException('Unable to write file ' . $path);
78
        }
79
80
        return new SplFileObject($path);
81
    }
82
83
    /**
84
     * Transform a class to a path.
85
     *
86
     * @param  string  $class
87
     * @return string
88
     */
89
    protected function classToPath($class)
90
    {
91
        $base = realpath(__DIR__ . '/../../..');
92
93
        return $base . '/' . dirname(str_replace('\\', '/', $class));
94
    }
95
96
    /**
97
     * Generate and return the file contents.
98
     *
99
     * @return string
100
     */
101
    protected function generate()
102
    {
103
        $lines = array_map(function ($line) {
104
            return implode('', $line);
105
        }, $this->getLines());
106
107
        $output = implode("\r\n", $lines);
108
        // $output = iconv('UTF-8', 'ASCII//TRANSLIT', $output);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
        $output = strtoupper($output);
110
111
        return $output;
112
    }
113
114
    /**
115
     * Parse and return the schema structure.
116
     *
117
     * @return array
118
     */
119
    protected function parseSchema()
120
    {
121
        $parsed = json_decode(file_get_contents($this->schemaPath()), true);
122
123
        if ($parsed === null) {
124
            throw new RuntimeException('Unable to parse schema');
125
        }
126
127
        return $parsed;
128
    }
129
130
    /**
131
     * Generate and return the schema file path.
132
     *
133
     * @return string
134
     */
135
    protected function schemaPath()
136
    {
137
        return realpath($this->classToPath(get_class($this)) . $this->schemaFile);
138
    }
139
}
140