Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Mail
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
1 / 1
6
 writeValidatedMail
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Mellon\Validator;
11use BO\Zmsentities\Mail as Entity;
12
13class Mail extends BaseController
14{
15    /**
16     * @SuppressWarnings(UnusedFormalParameter)
17     * @return \Psr\Http\Message\ResponseInterface
18     */
19    public function readResponse(
20        \Psr\Http\Message\RequestInterface $request,
21        \Psr\Http\Message\ResponseInterface $response,
22        array $args
23    ) {
24        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
25        $workstationRequest = new \BO\Zmsclient\WorkstationRequests(\App::$http, $workstation);
26        $selectedProcessId = Validator::param('selectedprocess')->isNumber()->getValue();
27        $success = Validator::param('success')->isString()->getValue();
28        $error = Validator::param('error')->isString()->getValue();
29        $dialog = Validator::param('dialog')->isNumber()->getValue();
30        $department = $workstationRequest->readDepartment();
31        $formResponse = null;
32        $input = $request->getParsedBody();
33        $process = ($selectedProcessId) ?
34            \App::$http->readGetResult('/process/' . $selectedProcessId . '/')->getEntity() :
35            null;
36        if (array_key_exists('submit', (array)$input) && 'form' == $input['submit']) {
37            $formResponse = $this->writeValidatedMail($process, $department);
38            if ($formResponse instanceof Entity) {
39                $query = [
40                    'selectedprocess' => $process->getId(),
41                    'dialog' => $dialog
42                ];
43                $message = ($formResponse->hasId()) ? ['success' => 'mail_sent'] : ['error' => 'mail_failed'];
44                $query = array_merge($message, $query);
45                return \BO\Slim\Render::redirect(
46                    'mail',
47                    [],
48                    $query
49                );
50            }
51        }
52
53        return \BO\Slim\Render::withHtml(
54            $response,
55            'page/mail.twig',
56            array(
57                'title' => 'eMail-Versand',
58                'menuActive' => $workstation->getVariantName(),
59                'workstation' => $workstation,
60                'department' => $department,
61                'process' => $process,
62                'success' => $success,
63                'error' => $error,
64                'dialog' => $dialog,
65                'form' => $formResponse,
66                'redirect' => $workstation->getVariantName()
67            )
68        );
69    }
70
71    private function writeValidatedMail($process, $department)
72    {
73        if (! $process->scope->hasEmailFrom()) {
74            throw new \BO\Zmsadmin\Exception\MailFromMissing();
75        }
76        $collection = array();
77        $collection['subject'] = Validator::param('subject')->isString()
78            ->isBiggerThan(2, "Es muss ein aussagekräftiger Betreff eingegeben werden");
79        $collection['message'] = Validator::param('message')->isString()
80            ->isBiggerThan(2, "Es muss eine aussagekräftige Nachricht eingegeben werden");
81        $collection = Validator::collection($collection);
82        if (! $collection->hasFailed()) {
83            $mail = (new Entity())->toCustomMessageEntity($process, $collection->getValues());
84            $mail = \App::$http->readPostResult('/mails/', $mail->withDepartment($department))->getEntity();
85            return $mail;
86        }
87        return $collection->getStatus();
88    }
89}