Lines 88.37% 38 / 43
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readResponse 100.00% 12 / 12 100.00% 1 / 1 1
 getLoggedInWorkstation 100.00% 21 / 21 100.00% 1 / 1 1
 testLoginHash 28.57% 2 / 7 0.00% 0 / 1 6.28
 [BO\Zmsbackend\Api\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
16class WorkstationLogin extends \BO\Zmsbackend\Api\BaseController
17{
18    /**
19     * @SuppressWarnings(Param)
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22    #[\Override]
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ) {
28        $validator = $request->getAttribute('validator');
29        $resolveReferences = $validator->getParameter('resolveReferences')->isNumber()->setDefault(1)->getValue();
30        $input = Validator::input()->isJson()->assertValid()->getValue();
31        $entity = new \BO\Zmsentities\Useraccount($input);
32        $entity->testValid();
33
34        \BO\Zmsbackend\Connection\Select::getWriteConnection();
35        $workstation = self::getLoggedInWorkstation($request, $entity, $resolveReferences);
36        \BO\Zmsbackend\Connection\Select::writeCommit(); // @codeCoverageIgnore
37
38        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
39        $message->data = $workstation;
40
41        $response = Render::withLastModified($response, time(), '0');
42        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
43        return $response;
44    }
45
46    public static function getLoggedInWorkstation(\Psr\Http\Message\RequestInterface $request, \BO\Zmsentities\Useraccount $entity, int $resolveReferences)
47    {
48        \BO\Zmsbackend\Helper\UserAuth::testUseraccountExists($entity->getId());
49        $useraccount = \BO\Zmsbackend\Helper\UserAuth::getVerifiedUseraccount($entity);
50        \BO\Zmsbackend\Helper\UserAuth::testPasswordMatching($useraccount, $entity->password);
51
52        $workstation = (new \BO\Zmsbackend\Helper\User($request, $resolveReferences))->readWorkstation();
53        \BO\Zmsbackend\Helper\User::testWorkstationIsOveraged($workstation);
54
55        static::testLoginHash($workstation);
56        $workstation = (new \BO\Zmsbackend\Workstation\Service\Workstation())->writeEntityLoginByName(
57            $useraccount->id,
58            $useraccount->password,
59            \App::getNow(),
60            (new \DateTime())->setTimestamp(time() + \App::SESSION_DURATION),
61            $resolveReferences
62        );
63
64        \BO\Zmsbackend\Log\Service\Log::writeLogEntry(
65            "LOGIN (WorkstattionLogin::getLoggedInWorkstation) " . $useraccount->id,
66            0,
67            \BO\Zmsbackend\Log\Service\Log::PROCESS,
68            $workstation->getScope()->getId(),
69            $workstation->getUseraccount()->getId()
70        );
71
72        return $workstation;
73    }
74
75    /**
76     * @return void
77     */
78    public static function testLoginHash($workstation)
79    {
80        $useraccount = $workstation->getUseraccount();
81        if (isset($useraccount->id)) {
82            $logInHash = (new \BO\Zmsbackend\Workstation\Service\Workstation())->readLoggedInHashByName($useraccount->id);
83            if (null !== $logInHash) {
84                $exception = new \BO\Zmsbackend\Useraccount\Exception\UserAlreadyLoggedIn();
85                $exception->data = $workstation;
86                throw $exception;
87            }
88        }
89    }
90}

Inherited from BO\Zmsbackend\Api\BaseController

20    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
21    {
22        $request = $this->initRequest($request);
23        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
24        return $this->readResponse($request, $noCacheResponse, $args);
25    }