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