RetourResolver   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 18
eloc 39
c 4
b 0
f 0
dl 0
loc 85
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveAll() 0 7 1
F resolve() 0 61 17
1
<?php
2
/**
3
 * Retour plugin for Craft CMS
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/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2019 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\gql\resolvers;
13
14
use Craft;
15
use craft\base\Element;
16
use craft\errors\SiteNotFoundException;
17
use craft\gql\base\Resolver;
18
use GraphQL\Type\Definition\ResolveInfo;
19
use nystudio107\retour\helpers\UrlHelper;
20
use nystudio107\retour\Retour;
21
use Throwable;
22
23
/**
24
 * Class RetourResolver
25
 *
26
 * @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...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
27
 * @package   Retour
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
28
 * @since     3.1.26
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
29
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
30
class RetourResolver extends Resolver
31
{
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
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...
33
     * @inheritDoc
34
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
35
    public static function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed
36
    {
37
        // If our source is an Element, extract the URI and siteId from it
38
        if ($source instanceof Element) {
39
            /** 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...
40
            $uri = $source->uri;
41
            $siteId = $source->siteId;
42
        } else {
43
            // Otherwise, use the passed in arguments, or defaults
44
            $uri = $arguments['uri'] ?? '/';
45
            $siteId = $arguments['siteId'] ?? null;
46
            if (isset($arguments['site'])) {
47
                $site = Craft::$app->getSites()->getSiteByHandle($arguments['site']);
48
                if ($site !== null) {
49
                    $siteId = $site->id;
50
                }
51
            }
52
        }
53
        $uri = trim($uri === '/' ? '__home__' : $uri);
54
55
        $redirect = null;
56
57
        // Strip the query string if `alwaysStripQueryString` is set
58
        if (Retour::$settings->alwaysStripQueryString) {
59
            $uri = UrlHelper::stripQueryString($uri);
60
        }
61
62
        if (!Retour::$plugin->redirects->excludeUri($uri)) {
0 ignored issues
show
Bug introduced by
The method excludeUri() does not exist on null. ( Ignorable by Annotation )

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

62
        if (!Retour::$plugin->redirects->/** @scrutinizer ignore-call */ excludeUri($uri)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            $redirect = Retour::$plugin->redirects->findRedirectMatch($uri, $uri, $siteId);
64
65
            if ($redirect === null && Craft::$app->getElements()->getElementByUri(trim($uri, '/'), $siteId) === null) {
66
                // Set the `site` virtual field
67
                $redirect['site'] = null;
68
                $redirect['siteId'] = $siteId;
69
                if (isset($redirect['siteId']) && (int)$redirect['siteId'] !== 0) {
70
                    $site = Craft::$app->getSites()->getSiteById((int)$redirect['siteId']);
71
                    if ($site !== null) {
72
                        $redirect['site'] = $site->handle;
73
                    }
74
                }
75
                // Increment the stats
76
                Retour::$plugin->statistics->incrementStatistics($uri, false, $siteId);
0 ignored issues
show
Bug introduced by
The method incrementStatistics() does not exist on null. ( Ignorable by Annotation )

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

76
                Retour::$plugin->statistics->/** @scrutinizer ignore-call */ 
77
                                             incrementStatistics($uri, false, $siteId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            }
78
        }
79
        if ($redirect !== null && isset($redirect['redirectDestUrl'])) {
80
            $dest = $redirect['redirectDestUrl'];
81
            $path = $redirect['redirectDestUrl'];
82
            // Combine the URL and path together, merging them as appropriate
83
            try {
84
                if (!UrlHelper::isAbsoluteUrl($dest) && !UrlHelper::pathHasSitePrefix($path)) {
85
                    $dest = UrlHelper::siteUrl('/', null, null, $siteId);
86
                    $dest = UrlHelper::mergeUrlWithPath($dest, $path);
87
                    $dest = parse_url($dest, PHP_URL_PATH);
88
                }
89
            } catch (Throwable $e) {
90
                // That's ok
91
            }
92
            $redirect['redirectDestUrl'] = $dest;
93
        }
94
95
        return $redirect;
96
    }
97
98
    /**
99
     * Return all static redirects for a site.
100
     *
101
     * @param $source
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
102
     * @param array $arguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
103
     * @param $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
104
     * @param ResolveInfo $resolveInfo
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
105
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
106
     * @throws SiteNotFoundException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
107
     */
108
    public static function resolveAll($source, array $arguments, $context, ResolveInfo $resolveInfo)
0 ignored issues
show
Unused Code introduced by
The parameter $source 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

108
    public static function resolveAll(/** @scrutinizer ignore-unused */ $source, array $arguments, $context, ResolveInfo $resolveInfo)

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 $resolveInfo 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

108
    public static function resolveAll($source, array $arguments, $context, /** @scrutinizer ignore-unused */ ResolveInfo $resolveInfo)

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

108
    public static function resolveAll($source, array $arguments, /** @scrutinizer ignore-unused */ $context, ResolveInfo $resolveInfo)

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...
109
    {
110
        $siteId = $arguments['siteId'] ?? Craft::$app->getSites()->getCurrentSite()->id;
111
112
        $redirects = Retour::$plugin->redirects->getAllStaticRedirects(null, $siteId);
113
114
        return $redirects;
115
    }
116
}
117