GetCraftQLSchema   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 70
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 49 6
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * @link      https://nystudio107.com/
6
 * @copyright Copyright (c) 2019 nystudio107
7
 * @license   https://nystudio107.com/license
8
 */
9
10
namespace nystudio107\seomatic\listeners;
11
12
use craft\base\Element;
13
use craft\helpers\Json;
14
use markhuot\CraftQL\Builders\Field as FieldBuilder;
15
use markhuot\CraftQL\Events\AlterSchemaFields;
16
use nystudio107\seomatic\helpers\Container as ContainerHelper;
17
use nystudio107\seomatic\models\MetaJsonLdContainer;
18
19
use nystudio107\seomatic\models\MetaLinkContainer;
20
use nystudio107\seomatic\models\MetaScriptContainer;
21
22
use nystudio107\seomatic\models\MetaTagContainer;
23
use nystudio107\seomatic\models\MetaTitleContainer;
24
25
/**
26
 * @author    nystudio107
27
 * @package   Seomatic
28
 * @since     3.2.0
29
 */
30
class GetCraftQLSchema
31
{
32
    // Constants
33
    // =========================================================================
34
35
    const CRAFT_QL_FIELDS = [
36
        'metaTitleContainer' => MetaTitleContainer::CONTAINER_TYPE,
37
        'metaTagContainer' => MetaTagContainer::CONTAINER_TYPE,
38
        'metaLinkContainer' => MetaLinkContainer::CONTAINER_TYPE,
39
        'metaScriptContainer' => MetaScriptContainer::CONTAINER_TYPE,
40
        'metaJsonLdContainer' => MetaJsonLdContainer::CONTAINER_TYPE,
41
    ];
42
43
    // Public Methods
44
    // =========================================================================
45
46
    /**
47
     * Add the `seomatic` root GraphQL field
48
     *
49
     * @param AlterSchemaFields $event
50
     */
51
    public static function handle(AlterSchemaFields $event)
52
    {
53
        // Create the root object
54
        $seomaticField = $event->schema->createObjectType('seomaticData');
55
        // Add in the CraftQL fields
56
        foreach (self::CRAFT_QL_FIELDS as $fieldHandle => $containerType) {
57
            $seomaticField
58
                ->addStringField($fieldHandle)
59
                ->resolve(function(array $data) use ($containerType) {
60
                    // $root contains the data returned by the field below
61
                    $result = ContainerHelper::getContainerArrays(
62
                        [$containerType],
63
                        $data['uri'],
64
                        $data['siteId'],
65
                        $data['asArray']
66
                    );
67
                    if (isset($result[$containerType]) && is_array($result[$containerType])) {
68
                        $result[$containerType] = Json::encode($result[$containerType]);
69
                    }
70
71
                    return $result[$containerType];
72
                });
73
        }
74
        // Add the root
75
        $event->schema->addField('seomatic')
76
            ->arguments(function(FieldBuilder $field) {
77
                $field->addIntArgument('siteId');
78
                $field->addStringArgument('uri');
79
                $field->addBooleanArgument('asArray');
80
            })
81
            ->type($seomaticField)
82
            ->resolve(function($root, $args, $context, $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $info is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
            ->resolve(function($root, $args, $context, /** @scrutinizer ignore-unused */ $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
            ->resolve(function($root, $args, /** @scrutinizer ignore-unused */ $context, $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
                // If our root is an Element, extract the URI and siteId from it
84
                if ($root instanceof Element) {
85
                    /** Element $root */
86
                    $uri = $root->uri;
87
                    $siteId = $root->siteId;
88
                } else {
89
                    // Otherwise use the passed in arguments, or defaults
90
                    $uri = $args['uri'] ?? '/';
91
                    $siteId = $args['siteId'] ?? null;
92
                }
93
                $asArray = $args['asArray'] ?? false;
94
                $uri = trim($uri === '/' ? '__home__' : $uri, '/');
95
96
                return [
97
                    'uri' => $uri,
98
                    'siteId' => $siteId,
99
                    'asArray' => $asArray,
100
                ];
101
            });
102
    }
103
}
104