Passed
Push — v3 ( 5fb596...084ffb )
by Andrew
24:11 queued 12:16
created

src/gql/resolvers/SeomaticResolver.php (10 issues)

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
The tag in position 1 should be the @package tag
Loading history...
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
The tag in position 3 should be the @author tag
Loading history...
28
 */
29
class SeomaticResolver extends Resolver
30
{
31
    /**
0 ignored issues
show
Parameter $source should have a doc-comment as per coding-style.
Loading history...
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Parameter $context should have a doc-comment as per coding-style.
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Doc comment short description must be on the first line
Loading history...
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