Passed
Branch v4 (7a8bf7)
by Andrew
20:17 queued 07:27
created

JsonLdController::actionGetTypeInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\controllers;
13
14
use craft\web\Controller;
15
use Exception;
16
use nystudio107\seomatic\helpers\Schema as SchemaHelper;
17
use nystudio107\seomatic\Seomatic;
18
use yii\web\Response;
19
20
/**
21
 * @author    nystudio107
22
 * @package   Seomatic
23
 * @since     3.0.0
24
 */
25
class JsonLdController extends Controller
26
{
27
    // Properties
28
    // =========================================================================
29
30
    /**
31
     * @inheritdoc
32
     */
33
    protected array|bool|int $allowAnonymous = [
34
        'get-type',
35
        'get-decomposed-type',
36
        'get-type-array',
37
        'get-type-menu',
38
        'get-single-type-menu',
39
        'get-type-tree',
40
    ];
41
42
    // Public Methods
43
    // =========================================================================
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function beforeAction($action): bool
49
    {
50
        if (!Seomatic::$settings->enableJsonLdEndpoint) {
51
            $this->allowAnonymous = false;
52
        }
53
54
        return parent::beforeAction($action);
55
    }
56
57
    /**
58
     * Get the fully composed schema type
59
     *
60
     * @param string $schemaType
61
     *
62
     * @return Response
63
     */
64
    public function actionGetType($schemaType): Response
65
    {
66
        return $this->asJson(SchemaHelper::getSchemaType($schemaType));
67
    }
68
69
    /**
70
     * Get the fully composed schema type
71
     *
72
     * @param string $schemaType
73
     * @return Response
74
     * @throws \Exception
75
     */
76
    public function actionGetTypeInfo($schemaType): Response
77
    {
78
        return $this->asJson([
79
            'schema' => SchemaHelper::getSchemaType($schemaType),
80
            'meta' => SchemaHelper::getTypeMetaInfo($schemaType),
81
        ]);
82
    }
83
84
    /**
85
     * Get the decomposed schema type
86
     *
87
     * @param string $schemaType
88
     *
89
     * @return Response
90
     */
91
    public function actionGetDecomposedType($schemaType): Response
92
    {
93
        return $this->asJson(SchemaHelper::getDecomposedSchemaType($schemaType));
94
    }
95
96
    /**
97
     * Get an array of schema types
98
     *
99
     * @param string $path
100
     *
101
     * @return Response
102
     */
103
    public function actionGetTypeArray($path): Response
104
    {
105
        try {
106
            $schemaArray = SchemaHelper::getSchemaArray($path);
107
        } catch (Exception $e) {
108
            $schemaArray = [];
109
        }
110
111
        return $this->asJson($schemaArray);
112
    }
113
114
    /**
115
     * Get a flattened menu of schema types as an array
116
     *
117
     * @param string $path
118
     *
119
     * @return Response
120
     */
121
    public function actionGetTypeMenu($path): Response
122
    {
123
        return $this->asJson(SchemaHelper::getTypeMenu($path));
124
    }
125
126
    /**
127
     * Return a single menu of schemas starting at $path
128
     *
129
     * @param string $path
130
     *
131
     * @return Response
132
     */
133
    public function actionGetSingleTypeMenu($path): Response
134
    {
135
        return $this->asJson(SchemaHelper::getSingleTypeMenu($path));
136
    }
137
138
    /**
139
     * Return a full nested tree of Schema.org types, pre-formatted for the treeselect component
140
     *
141
     * @return Response
142
     */
143
    public function actionGetTypeTree(): Response
144
    {
145
        return $this->asJson(SchemaHelper::getTypeTree());
146
    }
147
}
148