Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.46% covered (success)
98.46%
64 / 65
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Scope
98.46% covered (success)
98.46%
64 / 65
80.00% covered (warning)
80.00%
4 / 5
13
0.00% covered (danger)
0.00%
0 / 1
 readResponse
97.96% covered (success)
97.96%
48 / 49
0.00% covered (danger)
0.00%
0 / 1
6
 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
 writeUpdatedEntity
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 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        if (!$workstation->getUseraccount()->hasAnyPermission(['scope','restrictedscope'])) {
33            throw new \BO\Zmsentities\Exception\UserAccountMissingRights();
34        }
35        $entityId = Validator::value($args['id'])->isNumber()->getValue();
36        $entity = \App::$http
37            ->readGetResult('/scope/' . $entityId . '/', [
38                'resolveReferences' => 1,
39                'accessRights' => 'restrictedscope',
40                'gql' => Helper\GraphDefaults::getScope()
41            ])
42            ->getEntity();
43
44        $sourceList = $this->readSourceList();
45        $providerList = Helper\ProviderHandler::readProviderList($entity->getSource());
46        $currentSource = $this->readCurrentSource($entity->getSource());
47
48        $organisation = \App::$http->readGetResult('/scope/' . $entityId . '/organisation/')->getEntity();
49        $department = \App::$http->readGetResult('/scope/' . $entityId . '/department/')->getEntity();
50        $callDisplayImage = \App::$http->readGetResult('/scope/' . $entityId . '/imagedata/calldisplay/')->getEntity();
51        $input = $request->getParsedBody();
52        if ($request->getMethod() === 'POST') {
53            $result = $this->writeUpdatedEntity($input, $entityId);
54            if ($result instanceof Entity) {
55                if ($workstation->getUseraccount()->hasPermissions(['scope'])) {
56                    $this->writeUploadedImage($request, $entityId, $input);
57                }
58                return \BO\Slim\Render::redirect('scope', ['id' => $entityId], [
59                    'success' => 'scope_saved'
60                ]);
61            }
62        }
63
64        return \BO\Slim\Render::withHtml(
65            $response,
66            'page/scope.twig',
67            array(
68                'title' => 'Standort',
69                'menuActive' => 'owner',
70                'workstation' => $workstation,
71                'scope' => $entity,
72                'provider' => $entity->provider,
73                'organisation' => $organisation,
74                'department' => $department,
75                'providerList' => $providerList,
76                'sourceList' => $sourceList,
77                'source' => $currentSource,
78                'callDisplayImage' => $callDisplayImage,
79                'success' => $success,
80                'exception' => (isset($result)) ? $result : null
81            )
82        );
83    }
84
85    protected function readSourceList()
86    {
87        $sourceList = \App::$http->readGetResult('/source/')->getCollection();
88        return $sourceList;
89    }
90
91    protected function readCurrentSource($source)
92    {
93        $source = \App::$http->readGetResult('/source/' . $source . '/')->getEntity();
94        return $source;
95    }
96
97    /**
98     * @param \BO\Zmsentities\Scope $input scope entity, if used without ID, a new scope is created
99     * @param Number $entityId Might be the entity scope or department if called from DepartmentAddScope
100     */
101    protected function writeUpdatedEntity($input, $entityId = null)
102    {
103        $entity = (new Entity($input))->withCleanedUpFormData();
104        return $this->handleEntityWrite(function () use ($entity, $entityId) {
105            if ($entity->id) {
106                $entity->id = $entityId;
107                return \App::$http->readPostResult('/scope/' . $entity->id . '/', $entity)->getEntity();
108            }
109            return \App::$http
110                ->readPostResult('/department/' . $entityId . '/scope/', $entity)
111                ->getEntity();
112        });
113    }
114
115    protected function writeUploadedImage(\Psr\Http\Message\RequestInterface $request, $entityId, $input)
116    {
117        if (isset($input['removeImage']) && $input['removeImage']) {
118            \App::$http->readDeleteResult('/scope/' . $entityId . '/imagedata/calldisplay/');
119        } else {
120            (new Helper\FileUploader($request, 'uploadCallDisplayImage'))->writeUploadToScope($entityId);
121        }
122    }
123}