GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 8bca22...568bf9 )
by Gabriel
13:02 queued 05:02
created

RecordTrait::getFilesystemDiskName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
namespace ByTIC\Common\Records\Traits\Media\Files;
4
5
use ByTIC\Common\Records\Media\Files\Model as ModelFile;
6
7
/**
8
 * Class RecordTrait
9
 * @package ByTIC\Common\Records\Traits\Media\Files
10
 */
11
trait RecordTrait
12
{
13
    use \ByTIC\Common\Records\Traits\AbstractTrait\RecordTrait;
14
15
    /**
16
     * @var ModelFile[]
17
     */
18
    public $files = null;
19
20
    /**
21
     * @param ModelFile $file
22
     * @return string
23
     */
24
    public function getFileURL($file)
25
    {
26
        return $this->getUploadURL() . $file->getName();
0 ignored issues
show
Bug introduced by
It seems like getUploadURL() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
    }
28
29
    /**
30
     * @param $fileData
31
     * @return bool|ModelFile
32
     */
33
    public function uploadFile($fileData)
34
    {
35
        $file = $this->getNewFile();
36
37
        if ($file->upload($fileData)) {
38
            return $file;
39
        }
40
41
        return false;
42
    }
43
44
    /**
45
     * File factory
46
     *
47
     * @param null $type
48
     *
49
     * @return ModelFile
50
     */
51
    public function getNewFile($type = null)
52
    {
53
        $class = $this->getFileModelName($type);
54
        /** @var ModelFile $file */
55
        $file = new $class();
56
        $file->setModel($this);
57
        $file->setFilesystem($this->getFilesystemDisk());
0 ignored issues
show
Bug introduced by
The method getFilesystemDisk() does not exist on ByTIC\Common\Records\Tra...Media\Files\RecordTrait. Did you maybe mean getFiles()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
58
        return $file;
59
    }
60
61
    /**
62
     * @param null $type
63
     * @return string
64
     */
65
    public function getFileModelName($type = null)
66
    {
67
        $name = $this->isNamespaced() ? $this->getFileModelNamespaced($type) : $this->getFileModelNameDefault($type);
0 ignored issues
show
Bug introduced by
It seems like isNamespaced() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
69
        return $name;
70
    }
71
72
    /**
73
     * @param null $type
74
     * @return string
75
     */
76
    public function getFileModelNamespaced($type = null)
77
    {
78
        $type = $type ? $type : 'Generic';
79
80
        return $this->getManager()->getModelNamespace() . 'Files\\' . ucfirst($type);
81
    }
82
83
    /**
84
     * @param null $type
85
     * @return string
86
     */
87
    public function getFileModelNameDefault($type = null)
88
    {
89
        if ($type) {
90
            return $this->getManager()->getModel() . "_File_" . ucfirst($type);
91
        }
92
93
        return $this->getManager()->getModel() . "_File";
94
    }
95
96
    /**
97
     * @param $request
98
     * @return bool
99
     */
100
    public function removeFile($request)
101
    {
102
        $this->checkFiles();
103
104
        if ($this->files[$request['file']]) {
105
            $this->files[$request['file']]->delete();
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * Check if files have been inited
113
     *
114
     * @return ModelFile[]|null
115
     */
116
    public function checkFiles()
117
    {
118
        if ($this->files === null) {
119
            $this->findFiles();
120
        }
121
        return $this->files;
122
    }
123
124
    /**
125
     * Find files
126
     *
127
     * @return void
128
     */
129
    public function findFiles()
130
    {
131
        $files = $this->getFilesystemDisk()->listContents($this->getFilesPath());
0 ignored issues
show
Bug introduced by
The method getFilesystemDisk() does not exist on ByTIC\Common\Records\Tra...Media\Files\RecordTrait. Did you maybe mean getFiles()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
132
        foreach ($files as $fileData) {
133
            $this->addNewFileFromArray($fileData);
134
        }
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getFilesPath()
141
    {
142
        return '/files/' . $this->getManager()->getTable() . '/' . $this->id . '/';
143
    }
144
145
    /**
146
     * @param $data
147
     */
148
    public function addNewFileFromArray($data)
149
    {
150
        $file = $this->getNewFile();
151
        $file->setPath($data['path']);
152
        $this->appendFile($file);
153
    }
154
155
    /**
156
     * @param ModelFile $file
157
     */
158
    public function appendFile($file)
159
    {
160
        $this->files[$file->getName()] = $file;
161
    }
162
163
    /**
164
     * @return ModelFile[]
165
     */
166
    public function getFiles(): array
167
    {
168
        $this->checkFiles();
169
        return $this->files;
170
    }
171
172
    /**
173
     * @param array $files
174
     */
175
    public function setFiles($files = [])
176
    {
177
        if ($files) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $files of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
178
            foreach ($files as $name) {
179
                $file = $this->getNewFile();
180
                $file->setName($name);
181
182
                $this->files[$name] = $file;
183
            }
184
        }
185
    }
186
}
187