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

src/Controller/AttachmentsController.php (2 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;
3
4
use Cake\Event\Event;
5
use Cake\Filesystem\File;
6
use Cake\Network\Exception\NotFoundException;
7
use Cake\Network\Response;
8
9
class AttachmentsController extends AppController
10
{
11
12
    /**
13
     * BeforeFilter handle.
14
     *
15
     * @param Event $event The beforeFilter event that was fired.
16
     *
17
     * @return void
18
     */
19
    public function beforeFilter(Event $event)
20
    {
21
        parent::beforeFilter($event);
22
23
        $this->Auth->deny();
24
    }
25
26
    /**
27
     * Download an attachment realated to an article.
28
     *
29
     * @throws \Cake\Network\Exception\NotFoundException When it missing an arguments or when the file doesn't exist.
30
     *
31
     * @return \Cake\Network\Exception\NotFoundException
32
     *         \Cake\Network\Response
33
     */
34
    public function download()
35
    {
36
        $this->loadModel('Users');
37
        $user = $this->Users
38
            ->find()
39
            ->where([
40
                'Users.id' => $this->Auth->user('id')
41
            ])
42
            ->contain([
43
                'Groups' => function ($q) {
44
                    return $q->select(['id', 'is_staff']);
45
                }
46
            ])
47
            ->first();
48
49
        if (!isset($this->request->type) || is_null($user)) {
50
            throw new NotFoundException();
51
        }
52
53
        // To add a new case, you must add it to the route regex :
54
        // See config/Routes/base.php#L125
55
        switch (strtolower($this->request->type)) {
56
            case "blog":
57
                $this->loadModel('BlogAttachments');
58
                $attachment = $this->BlogAttachments->get($this->request->id);
59
60
                $file = new File($attachment->url);
61
62
                if (!$file->exists()) {
63
                    throw new NotFoundException();
64
                }
65
66
                $this->BlogAttachments->patchEntity($attachment, ['download' => $attachment->download + 1]);
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\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...
67
                $this->BlogAttachments->save($attachment);
0 ignored issues
show
The property BlogAttachments does not exist on object<App\Controller\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...
68
69
                return $this->response->withFile(
70
                    $file->path,
71
                    ['download' => true, 'name' => $attachment->name]
72
                );
73
        }
74
75
        return $this->response;
76
    }
77
}
78