Passed
Push — master ( 54b1a6...aec489 )
by Christian
10:37 queued 13s
created

CustomerValueResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 18 3
A supports() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer;
4
5
use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
6
use Shopware\Core\PlatformRequest;
7
use Shopware\Core\System\SalesChannel\SalesChannelContext;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
10
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
11
12
class CustomerValueResolver implements ArgumentValueResolverInterface
13
{
14
    public function supports(Request $request, ArgumentMetadata $argument): bool
15
    {
16
        return $argument->getType() === CustomerEntity::class;
17
    }
18
19
    public function resolve(Request $request, ArgumentMetadata $argument): iterable
20
    {
21
        $loginRequired = $request->attributes->get(PlatformRequest::ATTRIBUTE_LOGIN_REQUIRED);
22
23
        if (!$loginRequired instanceof LoginRequired) {
24
            $route = $request->attributes->get('_route');
25
26
            throw new \RuntimeException('Missing @LoginRequired annotation for route: ' . $route);
27
        }
28
29
        $context = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
30
        if (!$context instanceof SalesChannelContext) {
31
            $route = $request->attributes->get('_route');
32
33
            throw new \RuntimeException('Missing sales channel context for route ' . $route);
34
        }
35
36
        yield $context->getCustomer();
37
    }
38
}
39