DocumentsController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 11
dl 0
loc 85
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B actionDownload() 0 69 6
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\DownloadDocumentEvent;
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 DocumentsController extends AbstractController
26
{
27
    const EVENT_DOWNLOAD_DOCUMENT = 'downloadDocument';
28
29
    /**
30
     * @param string|null $id
31
     * @param bool $inline
32
     * @param bool|null $canDownload
33
     * @return \craft\web\Response|\yii\console\Response
34
     * @throws NotFoundHttpException
35
     * @throws UnauthorizedHttpException
36
     * @throws \yii\web\BadRequestHttpException
37
     * @throws \yii\web\HttpException
38
     * @throws \yii\web\RangeNotSatisfiableHttpException
39
     */
40
    public function actionDownload(string $id = null, bool $canDownload = null, bool $inline = false)
41
    {
42
        if ($canDownload !== true && !Force::getInstance()->getSettings()->enableDocumentDownloads) {
43
            throw new UnauthorizedHttpException("Unable to download attachment.");
44
        }
45
46
        $id = $id ?? Craft::$app->getRequest()->getRequiredBodyParam('id');
47
        $inline = (bool) ($inline ?? Craft::$app->getRequest()->getParam('inline', $inline));
48
49
        $result = (new ObjectCriteria())
50
            ->setObject('ContentVersion')
51
            ->setId($id)
52
            ->read();
53
54
        if ($result->getStatusCode() !== 200) {
55
            throw new NotFoundHttpException(
56
                Craft::t(
57
                    'salesforce',
58
                    'The document could not be found.'
59
                )
60
            );
61
        }
62
63
        $object = Json::decodeIfJson(
64
            $result->getBody()->getContents()
65
        );
66
67
        $result = (new UrlCriteria())
68
            ->setUrl($object['VersionData'])
69
            ->read();
70
71
        if ($result->getStatusCode() !== 200) {
72
            throw new NotFoundHttpException(
73
                Craft::t(
74
                    'salesforce',
75
                    'The document contents could not be found.'
76
                )
77
            );
78
        }
79
80
        $event = new DownloadDocumentEvent([
81
            'object' => $object,
82
            'fileName' => $object['Title'] . '.' . strtolower($object['FileExtension']),
83
            'fileContents' => $result->getBody()->getContents(),
84
            'canDownload' => $canDownload
85
        ]);
86
87
        $this->trigger(
88
            self::EVENT_DOWNLOAD_DOCUMENT,
89
            $event
90
        );
91
92
        if (!$event->canDownload) {
93
            throw new UnauthorizedHttpException(
94
                Craft::t(
95
                    'salesforce',
96
                    $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...
97
                )
98
            );
99
        }
100
101
        return Craft::$app->getResponse()->sendContentAsFile(
102
            $event->fileContents,
103
            $event->fileName,
104
            [
105
                'inline' => $inline
106
            ]
107
        );
108
    }
109
}
110