Test Failed
Push — master ( 045277...23cc6e )
by Alexey
05:33
created

File::process()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 46
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 35
nc 32
nop 0
dl 0
loc 46
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Mode File
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Exchange1c\Mode;
13
14
class File extends \Exchange1c\Mode {
15
16
    public function process() {
17
        $dir = $this->exchange->path;
18
        \Tools::createDir($dir);
19
        $file = new \Exchange1c\Exchange\File();
20
        $file->name = $_GET['filename'];
21
        $file->exchange_id = $this->exchange->id;
22
        $file->status = 'pending';
23
        $file->save();
24
25
        $filename = \Tools::parsePath($_GET['filename']);
26
        if (strpos($filename, '/') !== false) {
27
            $subDir = substr($filename, 0, strrpos($filename, "/") + 1);
28
            \Tools::createDir($dir . '/' . $subDir);
29
        }
30
        $text = '';
31
        if (false === file_put_contents($dir . '/' . $filename, file_get_contents("php://input"), FILE_APPEND)) {
32
            $text = 'Fail on save file: ' . $filename;
33
            $file->status = 'failure';
34
        } else {
35
            $file->size = ceil(filesize($dir . '/' . $filename));
36
            $file->name = $filename;
37
            $file->status = 'success';
38
        }
39
        if (strpos($filename, '1cbitrix') !== false) {
40
            $data = new \SimpleXMLElement(file_get_contents($dir . '/' . $filename));
41
            $orders = new \Exchange1c\Parser\Orders($data);
42
            $orders->process();
43
        }
44
        if ($file->status === 'success') {
45
            $pathinfo = pathinfo($filename);
46
            if ($pathinfo['extension'] === 'zip') {
47
                $zip = new \ZipArchive;
48
                if ($zip->open($dir . '/' . $filename) === TRUE) {
49
                    $zip->extractTo($dir);
50
                    $zip->close();
51
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
52
                    //comment for chained files working
53
                    //$text = 'Fail on unzip file: ' . $filename;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
54
                    //$file->status = 'failure';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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
                }
56
            }
57
        }
58
        $file->save();
59
        \App::$cur->exchange1c->response($file->status, $text, false);
60
        $this->end($file->status);
61
    }
62
63
}
64