Completed
Push — master ( d93525...41af6a )
by Fèvre
10s
created

src/Controller/Admin/AttachmentsController.php (16 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
use Cake\Filesystem\File;
6
use Cake\Routing\Router;
7
8
class AttachmentsController extends AppController
9
{
10
11
    /**
12
     * Display all attachments.
13
     *
14
     * @return \Cake\Network\Response
15
     */
16
    public function index()
17
    {
18
        $this->loadModel('BlogAttachments');
19
20
        $this->paginate = [
21
            'maxLimit' => 15
22
        ];
23
24
        $attachments = $this->BlogAttachments
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25
            ->find()
26
            ->contain([
27
                'BlogArticles' => function ($q) {
28
                    return $q->select([
29
                        'id',
30
                        'title'
31
                    ]);
32
                },
33
                'Users' => function ($q) {
34
                    return $q->find('short');
35
                }
36
            ])
37
            ->order([
38
                'BlogArticles.created' => 'desc'
39
            ]);
40
41
        $attachments = $this->paginate($attachments);
42
        $this->set(compact('attachments'));
43
    }
44
45
    /**
46
     * Add an attachment to an article.
47
     *
48
     * @return \Cake\Network\Response|void
49
     */
50
    public function add()
51
    {
52
        $this->loadModel('BlogAttachments');
53
        $this->loadModel('BlogArticles');
54
        $attachment = $this->BlogAttachments->newEntity($this->request->getParsedBody());
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
56
        if ($this->request->is('post')) {
57
            $article = $this->BlogArticles
0 ignored issues
show
The property BlogArticles does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
                ->find()
59
                ->contain([
60
                    'BlogAttachments'
61
                ])
62
                ->where([
63
                    'BlogArticles.id' => $this->request->getData('article_id')
64
                ])
65
                ->first();
66
67
            //Check if the article has already an attachment
68
            if (!is_null($article->blog_attachment)) {
69
                $this->Flash->error(
70
                    __d(
71
                        'admin',
72
                        'This article has already an attachment, you can edit it <a href="{0}" class="btn btn-sm btn-danger">here</a>.',
73
                        Router::url(['_name' => 'attachments-edit', 'id' => $article->blog_attachment->id])
74
                    )
75
                );
76
77
                return $this->redirect(['action' => 'index']);
78
            }
79
80
            $attachment->user_id = $this->Auth->user('id');
81
            $attachment->accessible('url_file', true);
82
83 View Code Duplication
            if ($newAttachment = $this->BlogAttachments->save($attachment)) {
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                $file = new File(WWW_ROOT . $newAttachment->url);
85
86
                $newAttachment->name = $file->name;
87
                $newAttachment->extension = '.' . $file->info()['extension'];
88
                $newAttachment->size = $file->info()['filesize'];
89
90
                $this->BlogAttachments->save($newAttachment);
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
92
                $this->Flash->success(__d('admin', 'Your attachment has been created successfully !'));
93
94
                return $this->redirect(['action' => 'index']);
95
            }
96
        }
97
98
        $articles = $this->BlogAttachments->BlogArticles->find('list');
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
        $this->set(compact('attachment', 'articles'));
100
    }
101
102
    /**
103
     * Edit an attachment.
104
     *
105
     * @return \Cake\Network\Response|void
106
     */
107
    public function edit()
108
    {
109
        $this->loadModel('BlogAttachments');
110
        $this->loadModel('BlogArticles');
111
112
        $attachment = $this->BlogAttachments
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
            ->find()
114
            ->where([
115
                'id' => $this->request->id
116
            ])
117
            ->first();
118
119
        //Check if the attachment is found.
120
        if (empty($attachment)) {
121
            $this->Flash->error(__d('admin', 'This attachment doesn\'t exist or has been deleted.'));
122
123
            return $this->redirect(['action' => 'index']);
124
        }
125
126
        if ($this->request->is(['put', 'post'])) {
127
            $this->BlogAttachments->patchEntity($attachment, $this->request->getParsedBody());
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
129
            //Check if the article has already an attachment
130
            $article = $this->BlogArticles
0 ignored issues
show
The property BlogArticles does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
131
                ->find()
132
                ->contain([
133
                    'BlogAttachments'
134
                ])
135
                ->where([
136
                    'BlogArticles.id' => $this->request->getData('article_id')
137
                ])
138
                ->first();
139
140
            if (!is_null($article->blog_attachment) && $article->blog_attachment->id != $this->request->id) {
141
                $this->Flash->error(
142
                    __d(
143
                        'admin',
144
                        'This article has already an attachment, you can edit it <a href="{0}" class="btn btn-sm btn-danger">here</a>.',
145
                        Router::url(['_name' => 'attachments-edit', 'id' => $article->blog_attachment->id])
146
                    )
147
                );
148
149
                return $this->redirect(['action' => 'index']);
150
            }
151
152
            $attachment->user_id = $this->Auth->user('id');
153
            $attachment->accessible('url_file', true);
154
155 View Code Duplication
            if ($editedAttachment = $this->BlogAttachments->save($attachment)) {
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
                $file = new File(WWW_ROOT . $editedAttachment->url);
157
158
                $editedAttachment->name = $file->name;
159
                $editedAttachment->extension = '.' . $file->info()['extension'];
160
                $editedAttachment->size = $file->info()['filesize'];
161
162
                $this->BlogAttachments->save($editedAttachment);
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
163
164
                $this->Flash->success(__d('admin', 'Your attachment has been edited successfully !'));
165
166
                return $this->redirect(['action' => 'index']);
167
            }
168
        }
169
170
        $articles = $this->BlogAttachments->BlogArticles->find('list');
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
171
        $this->set(compact('attachment', 'articles'));
172
    }
173
174
    /**
175
     * Delete an Attachment.
176
     *
177
     * @return \Cake\Network\Response
178
     */
179
    public function delete()
180
    {
181
        $this->loadModel('BlogAttachments');
182
183
        $attachment = $this->BlogAttachments
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
184
            ->find()
185
            ->where([
186
                'id' => $this->request->id
187
            ])
188
            ->first();
189
190
        //Check if the attachment is found.
191
        if (empty($attachment)) {
192
            $this->Flash->error(__d('admin', 'This attachment doesn\'t exist or has been deleted.'));
193
194
            return $this->redirect(['action' => 'index']);
195
        }
196
197
        if ($this->BlogAttachments->delete($attachment)) {
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\Ad...\AttachmentsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
198
            $this->Flash->success(__d('admin', 'This attachment has been deleted successfully !'));
199
200
            return $this->redirect(['action' => 'index']);
201
        }
202
203
        $this->Flash->error(__d('admin', 'Unable to delete this attachment.'));
204
205
        return $this->redirect(['action' => 'index']);
206
    }
207
}
208