Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UseraccountAdd
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 testEntity
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsbackend\Useraccount\Api;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsbackend\Useraccount\Service\Useraccount;
13
14class UseraccountAdd extends \BO\Zmsbackend\Api\BaseController
15{
16    /**
17     * @SuppressWarnings(Param)
18     * @return \Psr\Http\Message\ResponseInterface
19     */
20    #[\Override]
21    public function readResponse(
22        \Psr\Http\Message\RequestInterface $request,
23        \Psr\Http\Message\ResponseInterface $response,
24        array $args
25    ) {
26        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
27        (new \BO\Zmsbackend\Helper\User($request, $resolveReferences))->checkPermissions('useraccount');
28        $input = Validator::input()->isJson()->assertValid()->getValue();
29
30        $entity = new \BO\Zmsentities\Useraccount($input);
31        $this->testEntity($entity, $input);
32        \BO\Zmsbackend\Helper\User::testWorkstationAssignedRoles($entity);
33        \BO\Zmsbackend\Helper\User::testWorkstationAccessRights($entity);
34        $entity->password = $entity->getHash($entity->password);
35
36        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
37        $message->data = (new \BO\Zmsbackend\Useraccount\Service\Useraccount())->writeEntity($entity, $resolveReferences);
38
39        $response = Render::withLastModified($response, time(), '0');
40        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
41        return $response;
42    }
43
44    /**
45     * @return void
46     */
47    protected function testEntity(\BO\Zmsentities\Useraccount $entity, $input)
48    {
49        if (0 == count($input)) {
50            throw new \BO\Zmsbackend\Useraccount\Exception\UseraccountInvalidInput();
51        }
52        if (!isset($entity['departments'])) {
53            $entity->departments = [];
54        }
55        try {
56            $entity->testValid('de_DE', 1);
57        } catch (\Exception $exception) {
58            $exception->data['input'] = $input;
59            throw $exception;
60        }
61
62        if ((new \BO\Zmsbackend\Useraccount\Service\Useraccount())->readIsUserExisting($entity->id)) {
63            throw new \BO\Zmsbackend\Useraccount\Exception\UseraccountAlreadyExists();
64        }
65    }
66}