Passed
Push — v3 ( 102dec...3a0576 )
by Andrew
47:49 queued 34:00
created

RetourResolver::resolve()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 28
ccs 0
cts 21
cp 0
rs 8.8333
cc 7
nc 12
nop 4
crap 56
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/
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\gql\base\Resolver;
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
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...
26
 * @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...
27
 * @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...
28
 */
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...
29
class RetourResolver extends Resolver
30
{
31
    /**
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...
32
     * @inheritDoc
33
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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
        $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
            if ($redirect === null && Craft::$app->getElements()->getElementByUri(trim($uri, '/'), $siteId) === null) {
56
                // Increment the stats
57
                Retour::$plugin->statistics->incrementStatistics($uri, false);
58
            }
59
        }
60
61
        return $redirect;
62
    }
63
}
64