Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
69 / 69
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Scope
100.00% covered (success)
100.00%
69 / 69
100.00% covered (success)
100.00%
5 / 5
14
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
4
 readSourceList
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 readCurrentSource
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 testUpdateEntity
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
 writeUploadedImage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 *
5 * @package Zmsadmin
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsadmin;
11
12use BO\Zmsentities\Scope as Entity;
13use BO\Mellon\Validator;
14
15class Scope extends BaseController
16{
17    /**
18     *
19     * @return String
20     */
21    public function readResponse(
22        \Psr\Http\Message\RequestInterface $request,
23        \Psr\Http\Message\ResponseInterface $response,
24        array $args
25    ) {
26        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
27
28        $workstation = \App::$http->readGetResult('/workstation/', [
29            'resolveReferences' => 1,
30            'gql' => Helper\GraphDefaults::getWorkstation()
31        ])->getEntity();
32
33        $entityId = Validator::value($args['id'])->isNumber()->getValue();
34        $entity = \App::$http
35            ->readGetResult('/scope/' . $entityId . '/', [
36                'resolveReferences' => 1,
37                'accessRights' => 'scope',
38                'gql' => Helper\GraphDefaults::getScope()
39            ])
40            ->getEntity();
41
42        $sourceList = $this->readSourceList();
43        $providerList = Helper\ProviderHandler::readProviderList($entity->getSource());
44        $currentSource = $this->readCurrentSource($entity->getSource());
45
46        $organisation = \App::$http->readGetResult('/scope/' . $entityId . '/organisation/')->getEntity();
47        $department = \App::$http->readGetResult('/scope/' . $entityId . '/department/')->getEntity();
48        $callDisplayImage = \App::$http->readGetResult('/scope/' . $entityId . '/imagedata/calldisplay/')->getEntity();
49        $input = $request->getParsedBody();
50        if ($request->getMethod() === 'POST') {
51            $result = $this->testUpdateEntity($input, $entityId);
52            if ($result instanceof Entity) {
53                $this->writeUploadedImage($request, $entityId, $input);
54                return \BO\Slim\Render::redirect('scope', ['id' => $entityId], [
55                    'success' => 'scope_saved'
56                ]);
57            }
58        }
59
60        return \BO\Slim\Render::withHtml(
61            $response,
62            'page/scope.twig',
63            array(
64                'title' => 'Standort',
65                'menuActive' => 'owner',
66                'workstation' => $workstation,
67                'scope' => $entity,
68                'provider' => $entity->provider,
69                'organisation' => $organisation,
70                'department' => $department,
71                'providerList' => $providerList,
72                'sourceList' => $sourceList,
73                'source' => $currentSource,
74                'callDisplayImage' => $callDisplayImage,
75                'success' => $success,
76                'exception' => (isset($result)) ? $result : null
77            )
78        );
79    }
80
81    protected function readSourceList()
82    {
83        $sourceList = \App::$http->readGetResult('/source/')->getCollection();
84        return $sourceList;
85    }
86
87    protected function readCurrentSource($source)
88    {
89        $source = \App::$http->readGetResult('/source/' . $source . '/')->getEntity();
90        return $source;
91    }
92
93    /**
94     * @param \BO\Zmsentities\Scope $input scope entity, if used without ID, a new scope is created
95     * @param Number $entityId Might be the entity scope or department if called from DepartmentAddScope
96     */
97    protected function testUpdateEntity($input, $entityId = null)
98    {
99        $entity = (new Entity($input))->withCleanedUpFormData();
100        try {
101            if ($entity->id) {
102                $entity->id = $entityId;
103                $entity = \App::$http->readPostResult('/scope/' . $entity->id . '/', $entity)->getEntity();
104            } else {
105                $entity = \App::$http->readPostResult('/department/' . $entityId . '/scope/', $entity)
106                                     ->getEntity();
107            }
108        } catch (\BO\Zmsclient\Exception $exception) {
109            $template = Helper\TwigExceptionHandler::getExceptionTemplate($exception);
110            if (
111                '' != $exception->template
112                && \App::$slim->getContainer()->get('view')->getLoader()->exists($template)
113            ) {
114                return [
115                    'template' => $template,
116                    'data' => $exception->data
117                ];
118            }
119            throw $exception;
120        }
121        return $entity;
122    }
123
124    protected function writeUploadedImage(\Psr\Http\Message\RequestInterface $request, $entityId, $input)
125    {
126        if (isset($input['removeImage']) && $input['removeImage']) {
127            \App::$http->readDeleteResult('/scope/' . $entityId . '/imagedata/calldisplay/');
128        } else {
129            (new Helper\FileUploader($request, 'uploadCallDisplayImage'))->writeUploadToScope($entityId);
130        }
131    }
132}