Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
HostSwitchController
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 switch
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace App\Web\Controller;
6
7use App\Shared\Host\ActiveHostProvider;
8use Psr\Http\Message\ResponseInterface;
9use Psr\Http\Message\ServerRequestInterface;
10use Yiisoft\DataResponse\DataResponseFactoryInterface;
11use Yiisoft\Http\Header;
12use Yiisoft\Http\Status;
13
14 class HostSwitchController
15{
16    public function __construct(private DataResponseFactoryInterface $responseFactory) {}
17
18    public function switch(ServerRequestInterface $request, ActiveHostProvider $hostProvider): ResponseInterface
19    {
20        $body = $request->getParsedBody();
21        $hostId = is_array($body) ? ($body['host_id'] ?? null) : null;
22
23        if (empty($hostId)) {
24            $hostProvider->setActiveHostId(null);
25        } else {
26            $hostProvider->setActiveHostId((string) $hostId);
27        }
28
29        $referer = $request->getHeaderLine(Header::REFERER) ?: '/';
30
31        return $this->responseFactory
32            ->createResponse(null, Status::FOUND)
33            ->withHeader(Header::LOCATION, $referer);
34    }
35}