Passed
Push — develop ( ff7e62...18f528 )
by Andrew
07:05 queued 10s
created

SeomaticResolver::resolve()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 3
b 0
f 0
nc 6
nop 4
dl 0
loc 28
rs 9.0777
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) 2019 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\gql\resolvers;
13
14
use craft\base\Element;
15
use craft\gql\base\Resolver;
16
17
use craft\helpers\Json;
18
use GraphQL\Type\Definition\ResolveInfo;
19
use nystudio107\seomatic\gql\interfaces\SeomaticInterface;
20
use nystudio107\seomatic\helpers\Container as ContainerHelper;
21
22
/**
23
 * Class SeomaticResolver
24
 *
25
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
26
 * @package   Seomatic
27
 * @since     3.2.8
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
28
 */
29
class SeomaticResolver extends Resolver
30
{
31
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $source should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $context should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $resolveInfo should have a doc-comment as per coding-style.
Loading history...
32
     * @inheritDoc
33
     */
34
    public static function resolve($source, array $arguments, $context, ResolveInfo $resolveInfo)
35
    {
36
        // If our source is an Element, extract the URI and siteId from it
37
        if ($source instanceof Element) {
38
            /** Element $source */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Doc comment short description must be on the first line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
39
            $uri = $source->uri;
40
            $siteId = $source->siteId;
41
        } else {
42
            // Otherwise use the passed in arguments, or defaults
43
            $uri = $arguments['uri'] ?? '/';
44
            $siteId = $arguments['siteId'] ?? null;
45
        }
46
        $asArray = $arguments['asArray'] ?? false;
47
        $uri = trim($uri === '/' ? '__home__' : $uri, '/');
48
49
        $result = ContainerHelper::getContainerArrays(
50
            array_values(SeomaticInterface::GRAPH_QL_FIELDS),
51
            $uri,
52
            $siteId,
53
            $asArray
54
        );
55
        foreach ($result as $key => $value) {
56
            if (isset($value) && is_array($value)) {
57
                $result[$key] = Json::encode($value);
58
            }
59
        }
60
61
        return $result;
62
    }
63
}
64