TypeController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 47
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A actionSettings() 0 37 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-link
7
 */
8
9
namespace flipbox\craft\link\controllers;
10
11
use Craft;
12
use craft\web\Controller;
13
use flipbox\craft\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
     * @return Response
25
     * @throws HttpException
26
     * @throws \Twig_Error_Loader
27
     * @throws \yii\base\Exception
28
     * @throws \yii\web\BadRequestHttpException
29
     */
30
    public function actionSettings(): Response
31
    {
32
        $this->requirePostRequest();
33
        $this->requireAcceptsJson();
34
35
        $view = $this->getView();
36
37
        $type = Link::getInstance()->findType(
38
            Craft::$app->getRequest()->getRequiredBodyParam('type')
39
        );
40
41
        if (!$type) {
42
            throw new HttpException("Type not found");
43
        }
44
45
        // Allow explicit setting of the identifier
46
        if ($identifier = Craft::$app->getRequest()->getBodyParam('identifier')) {
47
            $type->setIdentifier($identifier);
48
        }
49
50
        $html = $view->renderTemplate(
51
            'link/_components/fieldtypes/Link/type',
52
            [
53
                'type' => $type,
54
                'namespace' => Craft::$app->getRequest()->getRequiredBodyParam('namespace')
55
            ]
56
        );
57
58
        return $this->asJson(
59
            [
60
            'label' => $type::displayName(),
61
            'paneHtml' => $html,
62
            'headHtml' => $view->getHeadHtml(),
63
            'footHtml' => $view->getBodyHtml(),
64
            ]
65
        );
66
    }
67
}
68