Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Scope
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
5 / 5
11
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
 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
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->writeUpdatedEntity($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 writeUpdatedEntity($input, $entityId = null)
98    {
99        $entity = (new Entity($input))->withCleanedUpFormData();
100        return $this->handleEntityWrite(function () use ($entity, $entityId) {
101            if ($entity->id) {
102                $entity->id = $entityId;
103                return \App::$http->readPostResult('/scope/' . $entity->id . '/', $entity)->getEntity();
104            }
105            return \App::$http
106                ->readPostResult('/department/' . $entityId . '/scope/', $entity)
107                ->getEntity();
108        });
109    }
110
111    protected function writeUploadedImage(\Psr\Http\Message\RequestInterface $request, $entityId, $input)
112    {
113        if (isset($input['removeImage']) && $input['removeImage']) {
114            \App::$http->readDeleteResult('/scope/' . $entityId . '/imagedata/calldisplay/');
115        } else {
116            (new Helper\FileUploader($request, 'uploadCallDisplayImage'))->writeUploadToScope($entityId);
117        }
118    }
119}