Zip   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 87
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createForPersonalData() 0 18 1
A __construct() 0 8 1
A path() 0 4 1
A size() 0 8 2
A open() 0 6 1
A add() 0 13 3
A close() 0 6 1
1
<?php
2
3
namespace Spatie\PersonalDataExport;
4
5
use Illuminate\Support\Str;
6
use Spatie\TemporaryDirectory\TemporaryDirectory;
7
use ZipArchive;
8
9
class Zip
10
{
11
    /** @var \ZipArchive */
12
    protected $zipFile;
13
14
    /** @var int */
15
    protected $fileCount = 0;
16
17
    /** @var string */
18
    protected $pathToZip;
19
20
    public static function createForPersonalData(
21
        PersonalDataSelection $personalDataSelection,
22
        TemporaryDirectory $temporaryDirectory): self
23
    {
24
        $zipFilenameParts = [
25
            $personalDataSelection->user->getKey(),
26
            now()->timestamp,
27
            Str::random(64),
28
        ];
29
30
        $zipFilename = implode('_', $zipFilenameParts).'.zip';
31
32
        $pathToZip = $temporaryDirectory->path($zipFilename);
33
34
        return (new static($pathToZip))
35
            ->add($personalDataSelection->files(), $temporaryDirectory->path())
36
            ->close();
37
    }
38
39
    public function __construct(string $pathToZip)
40
    {
41
        $this->zipFile = new ZipArchive();
42
43
        $this->pathToZip = $pathToZip;
44
45
        $this->open();
46
    }
47
48
    public function path(): string
49
    {
50
        return $this->pathToZip;
51
    }
52
53
    public function size(): int
54
    {
55
        if ($this->fileCount === 0) {
56
            return 0;
57
        }
58
59
        return filesize($this->pathToZip);
60
    }
61
62
    public function open(): self
63
    {
64
        $this->zipFile->open($this->pathToZip, ZipArchive::CREATE);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string|array $files
71
     * @param string $rootPath
72
     *
73
     * @return \Spatie\PersonalDataExport\Zip
74
     */
75
    public function add($files, $rootPath): self
76
    {
77
        foreach ($files as $file) {
0 ignored issues
show
Bug introduced by
The expression $files of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
78
            if (file_exists($file)) {
79
                $nameInZip = Str::after($file, $rootPath.'/');
80
81
                $this->zipFile->addFile($file, ltrim($nameInZip, DIRECTORY_SEPARATOR));
82
            }
83
            $this->fileCount++;
84
        }
85
86
        return $this;
87
    }
88
89
    public function close(): self
90
    {
91
        $this->zipFile->close();
92
93
        return $this;
94
    }
95
}
96