Completed
Pull Request — master (#178)
by Fèvre
09:09 queued 04:15
created

AttachmentsController::download()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 7.1977
c 0
b 0
f 0
cc 8
eloc 34
nc 7
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\Controller;
3
4
use Cake\Event\Event;
5
use Cake\Filesystem\File;
6
use Cake\Network\Exception\ForbiddenException;
7
use Cake\Network\Exception\NotFoundException;
8
use Cake\Network\Response;
9
10
class AttachmentsController extends AppController
11
{
12
13
    /**
14
     * BeforeFilter handle.
15
     *
16
     * @param Event $event The beforeFilter event that was fired.
17
     *
18
     * @return void
19
     */
20
    public function beforeFilter(Event $event)
21
    {
22
        parent::beforeFilter($event);
23
24
        $this->Auth->deny();
25
    }
26
27
    /**
28
     * Download an attachment realated to an article.
29
     *
30
     * @throws \Cake\Network\Exception\NotFoundException When it missing an arguments or when the file doesn't exist.
31
     * @throws \Cake\Network\Exception\ForbiddenException When the user is not premium.
32
     *
33
     * @return \Cake\Network\Exception\ForbiddenException
34
     *         \Cake\Network\Exception\NotFoundException
35
     *         \Cake\Network\Response
36
     */
37
    public function download()
38
    {
39
        $this->loadModel('Users');
40
41
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users 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...
42
            ->find()
43
            ->where([
44
                'Users.id' => $this->request->session()->read('Auth.User.id')
45
            ])
46
            ->contain([
47
                'Groups' => function ($q) {
48
                    return $q->select(['id', 'is_staff']);
49
                }
50
            ])
51
            ->first();
52
53
        if (is_null($user)) {
54
            throw new ForbiddenException();
55
        }
56
57
        if (!isset($this->request->type)) {
58
            throw new NotFoundException();
59
        }
60
61
        switch ($this->request->type) {
62
            case "blog":
63
                if (!$user->premium && !$user->group->is_staff) {
64
                    throw new ForbiddenException();
65
                }
66
                $this->loadModel('BlogAttachments');
67
68
                $attachment = $this->BlogAttachments->get($this->request->id);
0 ignored issues
show
Documentation introduced by
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...
69
70
                if (!$attachment) {
71
                    throw new NotFoundException();
72
                }
73
74
                $file = new File($attachment->url);
75
76
                if (!$file->exists()) {
77
                    throw new NotFoundException();
78
                }
79
80
                $this->response->file(
81
                    $file->path,
82
                    ['download' => true, 'name' => $attachment->name]
83
                );
84
85
                $this->BlogAttachments->patchEntity($attachment, ['download' => $attachment->download + 1]);
0 ignored issues
show
Documentation introduced by
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...
86
                $this->BlogAttachments->save($attachment);
0 ignored issues
show
Documentation introduced by
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...
87
                break;
88
89
            default:
90
                throw new NotFoundException();
91
        }
92
93
        return $this->response;
94
    }
95
}
96