Completed
Pull Request — master (#143)
by
unknown
02:35
created

ContactUsAction::post()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 2
1
<?php
2
3
namespace Web\Action;
4
5
use ContactUs\Service\ContactUsService;
6
use Std\FilterException;
7
use Zend\Diactoros\Response\HtmlResponse;
8
use Zend\Diactoros\Response\JsonResponse;
9
use Zend\Expressive\Router\RouterInterface;
10
use Zend\Expressive\Template\TemplateRendererInterface as Template;
11
use Psr\Http\Message\ResponseInterface as Response;
12
use Psr\Http\Message\ServerRequestInterface as Request;
13
14
/**
15
 * Class ContactUsAction
16
 *
17
 * @package Web\Action
18
 * @author  Djordje Stojiljkovic <[email protected]>
19
 */
20
class ContactUsAction
21
{
22
    /**
23
     * @var Template $template
24
     */
25
    protected $template;
26
27
    /**
28
     * @var ContactUsService $contactUsService
29
     */
30
    protected $contactUsService;
31
32
    /**
33
     * @var RouterInterface $router
34
     */
35
    protected $router;
36
37
    /**
38
     * ContactUsAction constructor.
39
     *
40
     * @param Template          $template
41
     * @param ContactUsService  $contactUsService
42
     * @param RouterInterface   $router
43
     */
44
    public function __construct(
45
        Template         $template,
46
        ContactUsService $contactUsService,
47
        RouterInterface  $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        $this->template = $template;
50
        $this->contactUsService = $contactUsService;
51
    }
52
53
    /**
54
     * Executed when action is invoked.
55
     *
56
     * @param Request       $request
57
     * @param Response      $response
58
     * @param callable      $next
59
     *
60
     * @throws \Exception
61
     *
62
     * @return mixed
63
     */
64
    public function __invoke(Request $request, Response $response, callable $next = null)
65
    {
66
        return new HtmlResponse($this->template->render('web::contact', [
67
            'layout' => 'layout/web'
68
        ]));
69
    }
70
71
    /**
72
     * @param   Request   $request
73
     * @param   Response  $response
74
     *
75
     * @return  Response
76
     *
77
     * @throws  FilterException
78
     * @throws \Exception
79
     */
80
    public function post(Request $request, Response $response)
81
    {
82
        $data = $request->getAttributes();
83
        if (!empty($data)) {
84
            try
85
            {
86
                $this->contactUsService->create($data);
87
                return $response
88
                    ->withStatus(200)
89
                    ->withHeader('Location', $this->router->generateUri('contact-us'))
90
                ;
91
92
            } catch (FilterException $fe) {
93
94
                return $response
95
                    ->withStatus(302)
96
                    ->withHeader('Location', $this->router->generateUri('contact-us'))
97
                ;
98
99
            } catch (\Exception $e) {
100
                throw $e;
101
            }
102
        }
103
    }
104
}