Completed
Push — master ( fbcba1...2125d9 )
by Nate
04:26
created

AttachmentsController::actionDownload()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 0
cts 59
cp 0
rs 8.0541
c 0
b 0
f 0
cc 6
nc 5
nop 3
crap 42

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
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\craft\salesforce\controllers;
10
11
use Craft;
12
use craft\helpers\Json;
13
use flipbox\craft\ember\controllers\AbstractController;
14
use flipbox\craft\salesforce\criteria\ObjectCriteria;
15
use flipbox\craft\salesforce\criteria\UrlCriteria;
16
use flipbox\craft\salesforce\events\DownloadAttachmentEvent;
17
use flipbox\craft\salesforce\Force;
18
use yii\web\NotFoundHttpException;
19
use yii\web\UnauthorizedHttpException;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.2.0
24
 */
25
class AttachmentsController extends AbstractController
26
{
27
    const EVENT_DOWNLOAD_ATTACHMENT = 'downloadAttachment';
28
29
    /**
30
     * @param string|null $id
31
     * @return \craft\web\Response|\yii\console\Response
32
     * @throws NotFoundHttpException
33
     * @throws UnauthorizedHttpException
34
     * @throws \yii\web\BadRequestHttpException
35
     * @throws \yii\web\HttpException
36
     * @throws \yii\web\RangeNotSatisfiableHttpException
37
     */
38
    public function actionDownload(string $id = null, bool $canDownload = null, bool $inline = false)
39
    {
40
        if ($canDownload !== true && !Force::getInstance()->getSettings()->enableDocumentDownloads) {
41
            throw new UnauthorizedHttpException("Unable to download attachment.");
42
        }
43
44
        $id = $id ?? Craft::$app->getRequest()->getRequiredBodyParam('id');
45
        $inline = (bool) ($inline ?? Craft::$app->getRequest()->getParam('inline', $inline));
46
47
        $result = (new ObjectCriteria())
48
            ->setObject('Attachment')
49
            ->setId($id)
50
            ->read();
51
52
        if ($result->getStatusCode() !== 200) {
53
            throw new NotFoundHttpException(
54
                Craft::t(
55
                    'salesforce',
56
                    'The document could not be found.'
57
                )
58
            );
59
        }
60
61
        $object = Json::decodeIfJson(
62
            $result->getBody()->getContents()
63
        );
64
65
        $result = (new UrlCriteria())
66
            ->setUrl($object['Body'])
67
            ->read();
68
69
        if ($result->getStatusCode() !== 200) {
70
            throw new NotFoundHttpException(
71
                Craft::t(
72
                    'salesforce',
73
                    'The document contents could not be found.'
74
                )
75
            );
76
        }
77
78
        $event = new DownloadAttachmentEvent([
79
            'object' => $object,
80
            'fileName' => $object['Name'],
81
            'fileContents' => $result->getBody()->getContents(),
82
            'canDownload' => $canDownload
83
        ]);
84
85
        $this->trigger(
86
            self::EVENT_DOWNLOAD_ATTACHMENT,
87
            $event
88
        );
89
90
        if (!$event->canDownload) {
91
            throw new UnauthorizedHttpException(
92
                Craft::t(
93
                    'salesforce',
94
                    $event->unauthorizedMessage
0 ignored issues
show
Documentation introduced by
$event->unauthorizedMessage is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
                )
96
            );
97
        }
98
99
        return Craft::$app->getResponse()->sendContentAsFile(
100
            $event->fileContents,
101
            $event->fileName,
102
            [
103
                'inline' => $inline
104
            ]
105
        );
106
    }
107
}
108