PostController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A index() 12 12 3
A edit() 22 22 2
A save() 21 21 4
A delete() 12 12 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
declare(strict_types=1);
4
5
namespace Article\Controller;
6
7
use Article\Entity\ArticleType;
8
use Article\Service\PostService;
9
use Category\Service\CategoryService;
10
use Std\AbstractController;
11
use Std\FilterException;
12
use Zend\Diactoros\Response\HtmlResponse;
13
use Zend\Expressive\Router\RouterInterface as Router;
14
use Zend\Expressive\Template\TemplateRendererInterface as Template;
15
use Zend\Http\PhpEnvironment\Request;
16
use Zend\Session\SessionManager;
17
18 View Code Duplication
class PostController 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...
19
{
20
    /**
21
     * @var Template
22
     */
23
    private $template;
24
25
    /**
26
     * @var PostService
27
     */
28
    private $postService;
29
30
    /**
31
     * @var SessionManager
32
     */
33
    private $session;
34
35
    /**
36
     * @var Router
37
     */
38
    private $router;
39
40
    /**
41
     * @var CategoryService
42
     */
43
    private $categoryService;
44
45
    /**
46
     * PostController constructor.
47
     *
48
     * @param Template        $template
49
     * @param Router          $router
50
     * @param PostService     $postService
51
     * @param SessionManager  $session
52
     * @param CategoryService $categoryService
53
     */
54
    public function __construct(
55
        Template $template,
56
        Router $router,
57
        PostService $postService,
58
        SessionManager $session,
59
        CategoryService $categoryService
60
    ) {
61
        $this->template = $template;
62
        $this->postService = $postService;
63
        $this->session = $session;
64
        $this->router = $router;
65
        $this->categoryService = $categoryService;
66
    }
67
68
    public function index(): HtmlResponse
69
    {
70
        $params = $this->request->getQueryParams();
71
        $page = isset($params['page']) ? $params['page'] : 1;
72
        $limit = isset($params['limit']) ? $params['limit'] : 15;
73
        $posts = $this->postService->fetchAllArticles($page, $limit);
74
75
        return new HtmlResponse($this->template->render(
76
            'article::post/index',
77
            ['list' => $posts, 'layout' => 'layout/admin'])
78
        );
79
    }
80
81
    /**
82
     * Add/Edit show form.
83
     *
84
     * @return \Psr\Http\Message\ResponseInterface
85
     */
86
    public function edit($errors = []): \Psr\Http\Message\ResponseInterface
87
    {
88
        $id = $this->request->getAttribute('id');
89
        $post = $this->postService->fetchSingleArticle($id);
90
        $categories = $this->categoryService->getAll(ArticleType::POST);
91
92
        if ($this->request->getParsedBody()) {
93
            $post = (object) ($this->request->getParsedBody() + (array) $post);
94
            $post->article_id = $id;
95
        }
96
97
        return new HtmlResponse(
98
            $this->template->render(
99
                'article::post/edit', [
100
                    'post'       => $post,
101
                    'categories' => $categories,
102
                    'errors'     => $errors,
103
                    'layout'     => 'layout/admin',
104
                ]
105
            )
106
        );
107
    }
108
109
    /**
110
     * Add/Edit article action.
111
     *
112
     * @throws FilterException if filter fails
113
     * @throws \Exception
114
     *
115
     * @return \Psr\Http\Message\ResponseInterface
116
     */
117
    public function save(): \Psr\Http\Message\ResponseInterface
118
    {
119
        try {
120
            $id = $this->request->getAttribute('id');
121
            $user = $this->session->getStorage()->user;
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend\Session\Storage\StorageInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122
            $data = $this->request->getParsedBody();
123
            $data += (new Request())->getFiles()->toArray();
124
125
            if ($id) {
126
                $this->postService->updateArticle($data, $id);
127
            } else {
128
                $this->postService->createArticle($user, $data);
129
            }
130
        } catch (FilterException $fe) {
131
            return $this->edit($fe->getArrayMessages());
132
        } catch (\Exception $e) {
133
            throw $e;
134
        }
135
136
        return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.posts'));
137
    }
138
139
    /**
140
     * Delete post by id.
141
     *
142
     * @throws \Exception
143
     *
144
     * @return \Psr\Http\Message\ResponseInterface
145
     */
146
    public function delete(): \Psr\Http\Message\ResponseInterface
147
    {
148
        try {
149
            $this->postService->deleteArticle($this->request->getAttribute('id'));
150
        } catch (\Exception $e) {
151
            throw $e;
152
        }
153
154
        return $this->response->withStatus(302)->withHeader(
155
            'Location', $this->router->generateUri('admin.posts', ['action' => 'index'])
156
        );
157
    }
158
}
159