Passed
Push — trunk ( aca8a0...2bfe38 )
by Christian
11:04 queued 13s
created

SeoResolver::resolve()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 60
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 39
nc 5
nop 3
dl 0
loc 60
rs 8.6737
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Seo;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\QueryBuilder;
7
use Shopware\Core\Framework\Log\Package;
8
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
9
use Shopware\Core\Framework\Uuid\Uuid;
10
11
/**
12
 * @phpstan-import-type ResolvedSeoUrl from AbstractSeoResolver
13
 */
14
#[Package('sales-channel')]
15
class SeoResolver extends AbstractSeoResolver
16
{
17
    /**
18
     * @internal
19
     */
20
    public function __construct(private readonly Connection $connection)
21
    {
22
    }
23
24
    public function getDecorated(): AbstractSeoResolver
25
    {
26
        throw new DecorationPatternException(self::class);
27
    }
28
29
    /**
30
     * @return ResolvedSeoUrl
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Content\Seo\ResolvedSeoUrl was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     */
32
    public function resolve(string $languageId, string $salesChannelId, string $pathInfo): array
33
    {
34
        $seoPathInfo = ltrim($pathInfo, '/');
35
36
        $query = (new QueryBuilder($this->connection))
37
            ->select('id', 'path_info pathInfo', 'is_canonical isCanonical', 'sales_channel_id salesChannelId')
38
            ->from('seo_url')
39
            ->where('language_id = :language_id')
40
            ->andWhere('(sales_channel_id = :sales_channel_id OR sales_channel_id IS NULL)')
41
            ->andWhere('seo_path_info = :seoPath')
42
            ->setParameter('language_id', Uuid::fromHexToBytes($languageId))
43
            ->setParameter('sales_channel_id', Uuid::fromHexToBytes($salesChannelId))
44
            ->setParameter('seoPath', $seoPathInfo);
45
46
        $query->setTitle('seo-url::resolve');
47
48
        $seoPaths = $query->executeQuery()->fetchAllAssociative();
49
50
        // sort seoPaths by filled salesChannelId, save file sort on SQL server
51
        usort($seoPaths, static function ($a, $b) {
52
            if ($a['salesChannelId'] === null) {
53
                return 1;
54
            }
55
            if ($b['salesChannelId'] === null) {
56
                return -1;
57
            }
58
59
            return 0;
60
        });
61
62
        $seoPath = $seoPaths[0] ?? ['pathInfo' => $seoPathInfo, 'isCanonical' => false];
63
64
        if (!$seoPath['isCanonical']) {
65
            $query = $this->connection->createQueryBuilder()
66
                ->select('path_info pathInfo', 'seo_path_info seoPathInfo')
67
                ->from('seo_url')
68
                ->where('language_id = :language_id')
69
                ->andWhere('sales_channel_id = :sales_channel_id')
70
                ->andWhere('path_info = :pathInfo')
71
                ->andWhere('is_canonical = 1')
72
                ->setMaxResults(1)
73
                ->setParameter('language_id', Uuid::fromHexToBytes($languageId))
74
                ->setParameter('sales_channel_id', Uuid::fromHexToBytes($salesChannelId))
75
                ->setParameter('pathInfo', '/' . ltrim((string) $seoPath['pathInfo'], '/'));
76
77
            // we only have an id when the hit seo url was not a canonical url, save the one filter condition
78
            if (isset($seoPath['id'])) {
79
                $query->andWhere('id != :id')
80
                    ->setParameter('id', $seoPath['id']);
81
            }
82
83
            $canonical = $query->executeQuery()->fetchAssociative();
84
            if ($canonical) {
85
                $seoPath['canonicalPathInfo'] = '/' . ltrim((string) $canonical['seoPathInfo'], '/');
86
            }
87
        }
88
89
        $seoPath['pathInfo'] = '/' . ltrim((string) $seoPath['pathInfo'], '/');
90
91
        return $seoPath;
92
    }
93
}
94