Issues (16)

app/Services/ImportService.php (8 issues)

1
<?php
2
declare(strict_types = 1);
3
4
namespace App\Services;
5
6
use Illuminate\Filesystem\Filesystem;
7
8
class ImportService
9
{
10
11
    /**
12
     * Validate files to import
13
     *
14
     * @param string|array $filePaths paths to text files to import
15
     * @return array validation errors
16
     *
17
     * @todo adapt validation error output in case of multiple file paths
18
     */
19
    public function validate($filePaths) : array
20
    {
21
22
        if (is_array($filePaths) === false) {
23
            $filePaths = [$filePaths];
24
        }
25
        
26
        foreach ($filePaths as $filePath) {
27
        
28
            if (is_string($filePath) === false ||
29
                file_exists($filePath) === false ||
0 ignored issues
show
$filePath of type array is incompatible with the type string expected by parameter $filename of file_exists(). ( Ignorable by Annotation )

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

29
                file_exists(/** @scrutinizer ignore-type */ $filePath) === false ||
Loading history...
30
                is_readable($filePath) === false ||
0 ignored issues
show
$filePath of type array is incompatible with the type string expected by parameter $filename of is_readable(). ( Ignorable by Annotation )

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

30
                is_readable(/** @scrutinizer ignore-type */ $filePath) === false ||
Loading history...
31
                is_file($filePath) === false
0 ignored issues
show
$filePath of type array is incompatible with the type string expected by parameter $filename of is_file(). ( Ignorable by Annotation )

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

31
                is_file(/** @scrutinizer ignore-type */ $filePath) === false
Loading history...
32
            ) {
33
                return ["Invalid file path"];
34
            }
35
36
            $fileSystem = new Filesystem;
37
            $fileMimeType = $fileSystem->mimeType($filePath);
0 ignored issues
show
$filePath of type array is incompatible with the type string expected by parameter $path of Illuminate\Filesystem\Filesystem::mimeType(). ( Ignorable by Annotation )

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

37
            $fileMimeType = $fileSystem->mimeType(/** @scrutinizer ignore-type */ $filePath);
Loading history...
38
            if ($fileMimeType !== "text/plain") {
39
                return ["Invalid file type"];
40
            }
41
42
            if (file($filePath) === false ||
0 ignored issues
show
$filePath of type array is incompatible with the type string expected by parameter $filename of file(). ( Ignorable by Annotation )

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

42
            if (file(/** @scrutinizer ignore-type */ $filePath) === false ||
Loading history...
43
                count(file($filePath)) === 0) {
0 ignored issues
show
It seems like file($filePath) can also be of type false; however, parameter $var of count() does only seem to accept Countable|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

43
                count(/** @scrutinizer ignore-type */ file($filePath)) === 0) {
Loading history...
44
                return ["Empty file"];
45
            }
46
        
47
        }
48
49
        return [];
50
51
    }
52
53
    /**
54
     * Import files
55
     *
56
     * @param string|array $filePaths paths to text files to import
57
     * @param string $class class of models to import
58
     */
59
    public function import($filePaths, string $class)
60
    {
61
62
        if (in_array("App\Models\Interfaces\Importable", class_implements($class)) === false) {
63
            throw new \InvalidArgumentException("Invalid import model class");
64
        }
65
66
        if (is_array($filePaths) === false) {
67
            $filePaths = [$filePaths];
68
        }
69
70
        $records = file(implode(PHP_EOL, $filePaths));
0 ignored issues
show
It seems like $filePaths can also be of type string; however, parameter $pieces of implode() 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

70
        $records = file(implode(PHP_EOL, /** @scrutinizer ignore-type */ $filePaths));
Loading history...
71
72
        // @todo add character check
73
        // @todo add uniqueness check
74
        // @todo add return of number of records imported
75
76
        // Data is cleaned.
77
        $records = array_unique(array_map("trim", $records));
0 ignored issues
show
It seems like $records can also be of type false; however, parameter $arr1 of array_map() 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

77
        $records = array_unique(array_map("trim", /** @scrutinizer ignore-type */ $records));
Loading history...
78
79
        foreach ($records as $record) {
80
            $model = new $class();
81
            $model->name = $record;
82
            $model->save();
83
        }
84
85
    }
86
87
}
88