Joiner::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
c 0
b 0
f 0
rs 10
nc 2
nop 2
cc 2
1
<?php
2
3
namespace VoidBuilder;
4
5
class File
6
{
7
    public $path;
8
9
    public function __construct (string $path)
10
    {
11
        if (!is_file ($path))
12
            throw new \Exception ('Wrong $path paeam');
13
        
14
        $this->path = $path;
15
    }
16
17
    public function toXML (): string
18
    {
19
        return '<File><Type>2</Type><Name>'. basename ($this->path) .'</Name><File>'. $this->path .'</File><ActiveX>False</ActiveX><ActiveXInstall>False</ActiveXInstall><Action>0</Action><OverwriteDateTime>False</OverwriteDateTime><OverwriteAttributes>False</OverwriteAttributes><PassCommandLine>False</PassCommandLine><HideFromDialogs>0</HideFromDialogs></File>';
20
    }
21
}
22
23
class Folder
24
{
25
    public $path;
26
    public $files = [];
27
28
    public function __construct (string $path)
29
    {
30
        if (!is_dir ($path))
31
            throw new \Exception ('Wrong $path param');
32
        
33
        $this->path = $path;
34
35
        foreach (array_slice (scandir ($path), 2) as $file)
0 ignored issues
show
Bug introduced by
It seems like scandir($path) can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($path), 2) as $file)
Loading history...
36
            $this->files[] = is_dir ($file = $path .'/'. $file) ?
37
                new Folder ($file) : new File ($file);
38
    }
39
40
    public function toXML (): string
41
    {
42
        return '<File><Type>3</Type><Name>'. basename ($this->path) .'</Name><Action>0</Action><OverwriteDateTime>False</OverwriteDateTime><OverwriteAttributes>False</OverwriteAttributes><HideFromDialogs>0</HideFromDialogs><Files>'. implode ('', array_map (function ($item)
43
        {
44
            return $item->toXML ();
45
        }, $this->files)) .'</Files></File>';
46
    }
47
}
48
49
class Joiner
50
{
51
    public $input;
52
    public $output;
53
    public $files = [];
54
55
    public function __construct (string $input, string $output)
56
    {
57
        if (!is_file ($input))
58
            throw new \Exception ('Wrong $input param');
59
60
        $this->input  = $input;
61
        $this->output = $output;
62
    }
63
64
    public function add (string $path): Joiner
65
    {
66
        $this->files[] = is_dir ($path) ?
67
            new Folder ($path) : new File ($path);
68
69
        return $this;
70
    }
71
72
    public function getEVB (): string
73
    {
74
        return str_replace ([
75
            '%INPUT_FILE%',
76
            '%OUTPUT_FILE%',
77
            '%FILES%'
78
        ], [
79
            $this->input,
80
            $this->output,
81
            implode ('', array_map (function ($item)
82
            {
83
                return $item->toXML ();
84
            }, $this->files))
85
        ], file_get_contents (dirname (__DIR__) .'/system/stub.evb'));
86
    }
87
88
    public function join (): string
89
    {
90
        file_put_contents (dirname (__DIR__) .'/system/temp.evb', $this->getEVB ());
91
        $return = shell_exec ('"'. dirname (__DIR__) .'/system/enigmavbconsole.exe" "system/temp.evb"');
92
        unlink (dirname (__DIR__) .'/system/temp.evb');
93
94
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
95
    }
96
}
97