Passed
Push — v3 ( 3d31db...9e8c74 )
by Andrew
29:00 queued 05:41
created

RetourResolver::resolve()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 24
rs 9.5222
cc 5
nc 8
nop 4
1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
9
 * @copyright Copyright (c) 2019 nystudio107
10
 */
11
12
namespace nystudio107\retour\gql\resolvers;
13
14
use craft\base\Element;
15
use craft\gql\base\Resolver;
16
use craft\helpers\Gql as GqlHelper;
17
18
use craft\helpers\UrlHelper;
19
use GraphQL\Type\Definition\ResolveInfo;
20
use nystudio107\retour\Retour;
21
22
/**
23
 * Class RetourResolver
24
 *
25
 * @author    nystudio107
26
 * @package   Retour
27
 * @since     3.1.26
28
 */
29
class RetourResolver extends Resolver
30
{
31
    /**
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 */
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
        $uri = trim($uri === '/' ? '__home__' : $uri);
47
48
        $redirect = null;
49
        // Strip the query string if `alwaysStripQueryString` is set
50
        if (Retour::$settings->alwaysStripQueryString) {
51
            $uri = UrlHelper::stripQueryString($uri);
52
        }
53
        if (!Retour::$plugin->redirects->excludeUri($uri)) {
54
            $redirect = Retour::$plugin->redirects->findRedirectMatch($uri, $uri, $siteId);
55
        }
56
57
        return $redirect;
58
    }
59
}
60