FilesHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 2
A extractZipFile() 0 15 1
A scanFolder() 0 10 2
1
<?php
2
3
namespace SnifferReport\Service;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Filesystem\Filesystem;
7
use \ZipArchive;
8
9
abstract class FilesHandler
10
{
11
12
    /**
13
     * Handles the given file
14
     *
15
     * @param $file_name
16
     * @param $mime_type
17
     *
18
     * @return array
19
     */
20
    public static function handle($file_name, $mime_type)
21
    {
22
        // @todo: add support for other types of compressed files.
23
        if ($mime_type == 'application/zip') {
24
            return self::extractZipFile($file_name);
25
        }
26
27
        return [FILES_DIRECTORY_ROOT . '/' . $file_name];
28
    }
29
30
    /**
31
     * Extracts the given zip file.
32
     *
33
     * @param $file_name
34
     *
35
     * @return array
36
     */
37
    private static function extractZipFile($file_name)
38
    {
39
        $zip_file_uri = FILES_DIRECTORY_ROOT . '/' . $file_name;
40
41
        $zip = new ZipArchive();
42
        $zip->open($zip_file_uri);
43
        $zip->extractTo(FILES_DIRECTORY_ROOT);
44
        $zip->close();
45
46
        // Remove zip file from the system.
47
        $fs = new Filesystem();
48
        $fs->remove($zip_file_uri);
49
50
        return self::scanFolder(FILES_DIRECTORY_ROOT);
51
    }
52
53
    /**
54
     * Scans folder to get all files inside.
55
     *
56
     * @param string $dir
57
     *
58
     * @return array
59
     */
60
    public static function scanFolder($dir)
61
    {
62
        $files = [];
63
        $finder = new Finder();
64
        $finder->files()->in($dir);
65
        foreach ($finder as $file) {
66
            $files[] = $file->getRealPath();
67
        }
68
        return $files;
69
    }
70
}
71