Passed
Push — master ( cd421c...e31be6 )
by Domenico
02:52
created

getFirst1024CharsFromXliff()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Matecat\XliffParser\XliffUtils;
4
5
use Exception;
6
use Matecat\XliffParser\Exception\NotSupportedVersionException;
7
use Matecat\XliffParser\Exception\NotValidFileException;
8
use Matecat\XliffParser\Utils\Files;
9
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckGlobalSight;
10
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckMateCATConverter;
11
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckSDL;
12
use Matecat\XliffParser\XliffUtils\CheckPipeline\CheckXliffVersion2;
13
14
class XliffProprietaryDetect {
15
    /**
16
     * @var array
17
     */
18
    protected static array $fileType = [];
19
20
    /**
21
     * @param string $xliffContent
22
     *
23
     * @return array
24
     */
25
    public static function getInfoFromXliffContent( string $xliffContent ): array {
26
        self::reset();
27
        $tmp = self::getFirst1024CharsFromXliff( $xliffContent );
28
29
        return self::getInfoFromTmp( $tmp );
30
    }
31
32
    /**
33
     * @param string $fullPathToFile
34
     *
35
     * @return array
36
     */
37
    public static function getInfo( string $fullPathToFile ): array {
38
        self::reset();
39
        $tmp                      = self::getFirst1024CharsFromXliff( null, $fullPathToFile );
40
        self::$fileType[ 'info' ] = Files::pathInfo( $fullPathToFile );
41
42
        return self::getInfoFromTmp( $tmp );
43
    }
44
45
    /**
46
     * @param array $tmp
47
     *
48
     * @return array
49
     */
50
    private static function getInfoFromTmp( array $tmp ): array {
51
        try {
52
            self::checkVersion( $tmp );
53
        } catch ( Exception $ignore ) {
54
            // do nothing
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
55
            // self::$fileType[ 'version' ] is left empty
56
        }
57
58
        // run CheckXliffProprietaryPipeline
59
        $pipeline = self::runPipeline( $tmp );
60
61
        self::$fileType[ 'proprietary' ]            = $pipeline[ 'proprietary' ];
62
        self::$fileType[ 'proprietary_name' ]       = $pipeline[ 'proprietary_name' ];
63
        self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ];
64
        self::$fileType[ 'converter_version' ]      = $pipeline[ 'converter_version' ];
65
66
        return self::$fileType;
67
    }
68
69
    /**
70
     * @param array|null $tmp
71
     *
72
     * @return array
73
     */
74
    private static function runPipeline( ?array $tmp = [] ): array {
75
        $pipeline = new CheckXliffProprietaryPipeline( $tmp );
76
        $pipeline->addCheck( new CheckSDL() );
77
        $pipeline->addCheck( new CheckGlobalSight() );
78
        $pipeline->addCheck( new CheckMateCATConverter() );
79
        $pipeline->addCheck( new CheckXliffVersion2() );
80
81
        return $pipeline->run();
82
    }
83
84
    /**
85
     * Reset $fileType
86
     */
87
    private static function reset() {
88
        self::$fileType = [
89
                'info'                   => [],
90
                'proprietary'            => false,
91
                'proprietary_name'       => null,
92
                'proprietary_short_name' => null
93
        ];
94
    }
95
96
    /**
97
     * @param string|null $stringData
98
     *
99
     * @return string
100
     */
101
    private static function getFirst1024CharsFromString( ?string $stringData ): string {
102
        if ( !empty( $stringData ) ) {
103
            return substr( $stringData, 0, 1024 );
104
        }
105
106
        return '';
107
    }
108
109
    /**
110
     * @param string|null $fullPathToFile
111
     *
112
     * @return string
113
     */
114
    private static function getFirst1024CharsFromFile( ?string $fullPathToFile ): string {
115
        $stringData = '';
116
        if ( !empty( $fullPathToFile ) && is_file( $fullPathToFile ) ) {
117
            $file_pointer = fopen( "$fullPathToFile", 'r' );
118
            // Checking Requirements (By specs, I know that xliff version is in the first 1KB)
119
            $stringData = fread( $file_pointer, 1024 );
120
            fclose( $file_pointer );
121
        }
122
123
        return $stringData;
124
125
    }
126
127
    /**
128
     * @param string|null $stringData
129
     * @param string|null $fullPathToFile
130
     *
131
     * @return string[]
132
     */
133
    private static function getFirst1024CharsFromXliff( ?string $stringData = null, string $fullPathToFile = null ): ?array {
134
        $stringData = static::getFirst1024CharsFromString( $stringData );
135
        if ( empty( $stringData ) ) {
136
            $stringData = static::getFirst1024CharsFromFile( $fullPathToFile );
137
        }
138
139
        return !empty( $stringData ) ? [ $stringData ] : [];
140
    }
141
142
    /**
143
     * @param array $tmp
144
     *
145
     * @throws NotSupportedVersionException
146
     * @throws NotValidFileException
147
     */
148
    protected static function checkVersion( array $tmp ) {
149
        if ( isset( $tmp[ 0 ] ) ) {
150
            self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] );
151
        }
152
    }
153
154
    /**
155
     * @param string $stringData
156
     *
157
     * @return array
158
     * @throws NotSupportedVersionException
159
     * @throws NotValidFileException
160
     */
161
    public static function getInfoByStringData( string $stringData ): array {
162
        self::reset();
163
164
        $tmp                      = self::getFirst1024CharsFromXliff( $stringData );
165
        self::$fileType[ 'info' ] = [];
166
        self::checkVersion( $tmp );
167
168
        // run CheckXliffProprietaryPipeline
169
        $pipeline = self::runPipeline( $tmp );
170
171
        self::$fileType[ 'proprietary' ]            = $pipeline[ 'proprietary' ];
172
        self::$fileType[ 'proprietary_name' ]       = $pipeline[ 'proprietary_name' ];
173
        self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ];
174
        self::$fileType[ 'converter_version' ]      = $pipeline[ 'converter_version' ];
175
176
        return self::$fileType;
177
    }
178
179
    /**
180
     * @param string      $fullPath
181
     * @param boolean     $enforceOnXliff
182
     * @param string|null $filterAddress
183
     *
184
     * @return bool|int
185
     */
186
    public static function fileMustBeConverted( string $fullPath, bool $enforceOnXliff = false, ?string $filterAddress = null ) {
187
        $convert = true;
188
189
        $fileType       = self::getInfo( $fullPath );
190
        $memoryFileType = Files::getMemoryFileType( $fullPath );
191
192
        if ( Files::isXliff( $fullPath ) || $memoryFileType ) {
193
            if ( !empty( $filterAddress ) ) {
194
195
                //conversion enforce
196
                if ( !$enforceOnXliff ) {
197
198
                    //if file is not proprietary AND Enforce is disabled
199
                    //we take it as is
200
                    if ( !$fileType[ 'proprietary' ] || $memoryFileType ) {
201
                        $convert = false;
202
                        //ok don't convert a standard sdlxliff
203
                    }
204
                } else {
205
                    //if conversion enforce is active
206
                    //we force all xliff files but not files produced by SDL Studio because we can handle them
207
                    if ( in_array( $fileType[ 'proprietary_short_name' ], [ 'matecat_converter', 'trados', 'xliff_v2' ] ) || $memoryFileType ) {
208
                        $convert = false;
209
                        //ok don't convert a standard sdlxliff
210
                    }
211
                }
212
            } elseif ( $fileType[ 'proprietary' ] ) {
213
214
                /**
215
                 * Application misconfiguration.
216
                 * upload should not be happened, but if we are here, raise an error.
217
                 * @see upload.class.php
218
                 * */
219
220
                $convert = -1;
221
                //stop execution
222
            } else {
223
                $convert = false;
224
                //ok don't convert a standard sdlxliff
225
            }
226
        }
227
228
        return $convert;
229
    }
230
}
231