PersonalDataSelection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A files() 0 4 1
A forUser() 0 6 1
A add() 0 16 2
A addFile() 0 6 2
A copyLocalFile() 0 14 1
A copyFileFromDisk() 0 14 1
A ensureDoesNotOverwriteExistingFile() 0 6 2
1
<?php
2
3
namespace Spatie\PersonalDataExport;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Facades\Storage;
7
use Spatie\PersonalDataExport\Exceptions\CouldNotAddToPersonalDataSelection;
8
use Spatie\TemporaryDirectory\TemporaryDirectory;
9
10
class PersonalDataSelection
11
{
12
    /** @var \Spatie\TemporaryDirectory\TemporaryDirectory */
13
    protected $temporaryDirectory;
14
15
    /** @var array */
16
    protected $files = [];
17
18
    /** @var \Illuminate\Database\Eloquent\Model */
19
    public $user;
20
21
    public function __construct(TemporaryDirectory $temporaryDirectory)
22
    {
23
        $this->temporaryDirectory = $temporaryDirectory;
24
    }
25
26
    public function files(): array
27
    {
28
        return $this->files;
29
    }
30
31
    public function forUser(ExportsPersonalData $user)
32
    {
33
        $this->user = $user;
0 ignored issues
show
Documentation Bug introduced by
$user is of type object<Spatie\PersonalDa...rt\ExportsPersonalData>, but the property $user was declared to be of type object<Illuminate\Database\Eloquent\Model>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param string $nameInDownload
40
     * @param array|string $content
41
     *
42
     * @return \Spatie\PersonalDataExport\PersonalDataSelection
43
     */
44
    public function add(string $nameInDownload, $content)
45
    {
46
        if (! is_string($content)) {
47
            $content = json_encode($content, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
48
        }
49
50
        $path = $this->temporaryDirectory->path($nameInDownload);
51
52
        $this->ensureDoesNotOverwriteExistingFile($path);
53
54
        $this->files[] = $path;
55
56
        file_put_contents($path, $content);
57
58
        return $this;
59
    }
60
61
    public function addFile(string $pathToFile, string $diskName = null)
62
    {
63
        return is_null($diskName)
64
            ? $this->copyLocalFile($pathToFile)
65
            : $this->copyFileFromDisk($pathToFile, $diskName);
66
    }
67
68
    protected function copyLocalFile(string $pathToFile)
69
    {
70
        $fileName = pathinfo($pathToFile, PATHINFO_BASENAME);
71
72
        $destination = $this->temporaryDirectory->path($fileName);
73
74
        $this->ensureDoesNotOverwriteExistingFile($destination);
75
76
        (new Filesystem())->copy($pathToFile, $destination);
77
78
        $this->files[] = $destination;
79
80
        return $this;
81
    }
82
83
    protected function copyFileFromDisk(string $pathOnDisk, string $diskName)
84
    {
85
        $stream = Storage::disk($diskName)->readStream($pathOnDisk);
86
87
        $pathInTemporaryDirectory = $this->temporaryDirectory->path($pathOnDisk);
88
89
        $this->ensureDoesNotOverwriteExistingFile($pathInTemporaryDirectory);
90
91
        file_put_contents($pathInTemporaryDirectory, stream_get_contents($stream), FILE_APPEND);
92
93
        $this->files[] = $pathInTemporaryDirectory;
94
95
        return $this;
96
    }
97
98
    protected function ensureDoesNotOverwriteExistingFile(string $path)
99
    {
100
        if (file_exists($path)) {
101
            throw CouldNotAddToPersonalDataSelection::fileAlreadyAddedToPersonalDataSelection($path);
102
        }
103
    }
104
}
105