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

ContactUsController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 133
loc 133
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A index() 16 16 3
A edit() 23 23 2
B save() 24 24 4
A delete() 19 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ContactUs\Controller;
4
5
use ContactUs\Entity\ContactUs;
6
use ContactUs\Service\ContactUsService;
7
use Std\FilterException;
8
use Zend\Diactoros\Response\HtmlResponse;
9
use Zend\Expressive\Router\RouterInterface as Router;
10
use Zend\Expressive\Template\TemplateRendererInterface as Template;
11
use Psr\Http\Message\ResponseInterface;
12
use Std\AbstractController;
13
14
/**
15
 * Class ContactUsController
16
 *
17
 * @package ContactUs
18
 * @author  Djordje Stojiljkovic <[email protected]>
19
 */
20 View Code Duplication
class ContactUsController extends AbstractController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * @var \Zend\Expressive\Router\RouterInterface $router
24
     */
25
    protected $router;
26
27
    /**
28
     * @var \Zend\Expressive\Template\TemplateRendererInterface $template
29
     */
30
    protected $template;
31
32
    /**
33
     * @var \ContactUs\Service\ContactUsService $contactUsService
34
     */
35
    protected $contactUsService;
36
37
    /**
38
     * ContactUsController constructor.
39
     *
40
     * @param Template          $template
41
     * @param Router            $router
42
     * @param ContactUsService  $contactUsService
43
     */
44
    public function __construct(Template $template, Router $router, ContactUsService $contactUsService)
45
    {
46
        $this->template = $template;
47
        $this->router = $router;
48
        $this->contactUsService = $contactUsService;
49
    }
50
51
    /**
52
     * @return HtmlResponse
53
     */
54
    public function index(): HtmlResponse
55
    {
56
        $params     = $this->request->getQueryParams();
57
        $page       = isset($params['page'])  ? $params['page']  : 1;
58
        $limit      = isset($params['limit']) ? $params['limit'] : 15;
59
        $pagination = $this->contactUsService->getPagination($page, $limit);
60
61
        return new HtmlResponse(
62
            $this->template->render(
63
                'contact-us::index', [
64
                    'pagination' => $pagination,
65
                    'layout'     => 'layout/admin',
66
                ]
67
            )
68
        );
69
    }
70
71
    /**
72
     * @param array $errors
73
     *
74
     * @return HtmlResponse
75
     */
76
    public function edit($errors = []): HtmlResponse
77
    {
78
        $id        = $this->request->getAttribute('id');
79
        $contactUs = $this->contactUsService->getById($id);
80
81
        if ($this->request->getParsedBody()) {
82
            $contactUs = new ContactUs();
83
            $contactUs->exchangeArray(
84
                $this->request->getParsedBody() + (array) $contactUs
85
            );
86
            $contactUs->setContactId($id);
87
        }
88
89
        return new HtmlResponse(
90
            $this->template->render(
91
                'contact-us::edit', [
92
                    'contact'   => $contactUs,
93
                    'errors'    => $errors,
94
                    'layout'    => 'layout/admin',
95
                ]
96
            )
97
        );
98
    }
99
100
    /**
101
     * @return ResponseInterface
102
     *
103
     * @throws \Exception
104
     */
105
    public function save(): ResponseInterface
106
    {
107
        try
108
        {
109
            $id   = $this->request->getAttribute('id');
110
            $data = $this->request->getParsedBody();
111
112
            if ($id) {
113
                $this->contactUsService->update($data, $id);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->request->getParsedBody() on line 110 can also be of type null or object; however, ContactUs\Service\ContactUsService::update() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
            } else {
115
                $this->contactUsService->create($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->request->getParsedBody() on line 110 can also be of type null or object; however, ContactUs\Service\ContactUsService::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
            }
117
118
            return $this->response
119
                ->withStatus(302)
120
                ->withHeader('Location', $this->router->generateUri('admin.contact-us'))
121
            ;
122
123
        } catch (FilterException $fe) {
124
            return $this->edit($fe->getArrayMessages());
125
        } catch (\Exception $e) {
126
            throw $e;
127
        }
128
    }
129
130
    /**
131
     * @return ResponseInterface
132
     */
133
    public function delete(): ResponseInterface
134
    {
135
        try
136
        {
137
            $id = $this->request->getAttribute('id');
138
            $this->contactUsService->delete($id);
139
140
            return $this->response
141
                ->withStatus(302)
142
                ->withHeader('Location', $this->router->generateUri('admin.contact-us'))
143
            ;
144
145
        } catch (\Exception $e) {
146
            return $this->response
147
                ->withStatus(302)
148
                ->withHeader('Location', $this->router->generateUri('admin.contact-us'))
149
            ;
150
        }
151
    }
152
}