TypeController::actionSettings()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 28
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/link/license
6
 * @link       https://www.flipboxfactory.com/software/link/
7
 */
8
9
namespace flipbox\link\controllers;
10
11
use Craft;
12
use craft\web\Controller;
13
use flipbox\link\Link;
14
use yii\web\HttpException;
15
use yii\web\Response;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class TypeController extends Controller
22
{
23
24
    /**
25
     * @return Response
26
     * @throws HttpException
27
     */
28
    public function actionSettings(): Response
29
    {
30
        $this->requirePostRequest();
31
        $this->requireAcceptsJson();
32
33
        $view = $this->getView();
34
35
        $type = Link::getInstance()->getType()->find(
36
            Craft::$app->getRequest()->getRequiredBodyParam('type')
37
        );
38
39
        if (!$type) {
40
            throw new HttpException("Type not found");
41
        }
42
43
        // Allow explicit setting of the identifier
44
        if ($identifier = Craft::$app->getRequest()->getBodyParam('identifier')) {
45
            $type->setIdentifier($identifier);
46
        }
47
48
        $html = $view->renderTemplate(
49
            'link/_components/fieldtypes/Link/type',
50
            [
51
                'type' => $type,
52
                'namespace' => Craft::$app->getRequest()->getRequiredBodyParam('namespace')
53
            ]
54
        );
55
56
        return $this->asJson([
57
            'label' => $type::displayName(),
58
            'paneHtml' => $html,
59
            'headHtml' => $view->getHeadHtml(),
60
            'footHtml' => $view->getBodyHtml(),
61
        ]);
62
    }
63
}
64