PostController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 20.71 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 10
Bugs 1 Features 7
Metric Value
wmc 10
c 10
b 1
f 7
lcom 1
cbo 8
dl 29
loc 140
ccs 0
cts 68
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A index() 0 14 2
A post() 0 12 2
A category() 0 18 2
A tag() 14 14 1
A search() 15 15 1
A notFound() 0 6 1

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 Albert221\Blog\Controller;
4
5
use Albert221\Blog\Pagination\PaginatorBuilder;
6
use Albert221\Blog\Repository\PostRepositoryInterface;
7
use League\Route\Http\Exception\NotFoundException;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Zend\Diactoros\Request;
10
use Zend\Diactoros\Response;
11
12
class PostController extends AbstractWidgetController
13
{
14
    /**
15
     * @var PostRepositoryInterface Posts
16
     */
17
    protected $posts;
18
19
    /**
20
     * @var PaginatorBuilder Paginator builder
21
     */
22
    protected $paginatorBuilder;
23
24
    public function __construct(
25
        PostRepositoryInterface $posts,
26
        PaginatorBuilder $paginatorBuilder
27
    ) {
28
        $this->posts = $posts;
29
        $this->paginatorBuilder = $paginatorBuilder;
30
    }
31
32
    /**
33
     * /
34
     *
35
     * @param  ServerRequestInterface $request
36
     *
37
     * @return string
38
     */
39
    public function index(ServerRequestInterface $request)
40
    {
41
        $this->provideWidgets();
42
43
        if (isset($request->getQueryParams()['q'])) {
44
            return $this->search($request, $request->getQueryParams()['q']);
45
        }
46
47
        $paginator = $this->paginatorBuilder->build($request, $this->posts->count());
48
49
        $posts = $this->posts->paginated($paginator->getCriteria());
50
51
        return $this->view('index.twig', compact('posts', 'paginator'));
52
    }
53
54
    /**
55
     * /{slug}
56
     *
57
     * @param string $slug
58
     * @return string
59
     * @throws NotFoundException when post does not exist
60
     */
61
    public function post($slug)
62
    {
63
        $this->provideWidgets();
64
65
        $post = $this->posts->bySlug($slug);
66
67
        if (!$post) {
68
            return $this->notFound();
69
        }
70
        
71
        return $this->view('post.twig', compact('post'));
72
    }
73
74
    /**
75
     * /kategoria/{slug}
76
     *
77
     * @param ServerRequestInterface $request
78
     * @param string $slug
79
     * @return string
80
     */
81
    public function category(ServerRequestInterface $request, $slug)
82
    {
83
        $this->provideWidgets();
84
85
        $paginator = $this->paginatorBuilder->build($request, $this->posts->byCategoryCount($slug));
86
87
        $posts = $this->posts->byCategory($slug, $paginator->getCriteria());
88
        
89
        if (empty($posts)) {
90
            return $this->notFound();
91
        }
92
93
        return $this->view('index.twig', [
94
            'posts' => $posts,
95
            'paginator' => $paginator,
96
            'title' => 'Posty w kategorii \''.$posts[0]->getCategory()->getName().'\''
97
        ]);
98
    }
99
100
    /**
101
     * /tag/{slug}
102
     *
103
     * @param ServerRequestInterface $request
104
     * @param string $slug
105
     * @return string
106
     */
107 View Code Duplication
    public function tag(ServerRequestInterface $request, $slug)
0 ignored issues
show
Duplication introduced by
This method 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...
108
    {
109
        $this->provideWidgets();
110
111
        $paginator = $this->paginatorBuilder->build($request, $this->posts->byTagCount($slug));
112
113
        $posts = $this->posts->byTag($slug, $paginator->getCriteria());
114
115
        return $this->view('index.twig', [
116
            'posts' => $posts,
117
            'paginator' => $paginator,
118
            'title' => 'Posty otagowane \''.$slug.'\''
119
        ]);
120
    }
121
122
    /**
123
     * /?q={term}
124
     *
125
     * @param ServerRequestInterface $request
126
     * @param string $term
127
     * @return string
128
     */
129 View Code Duplication
    public function search(ServerRequestInterface $request, $term)
0 ignored issues
show
Duplication introduced by
This method 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...
130
    {
131
        $this->provideWidgets();
132
133
        $paginator = $this->paginatorBuilder->build($request, $this->posts->searchCount($term));
134
135
        $posts = $this->posts->search($term, $paginator->getCriteria());
136
137
        return $this->view('index.twig', [
138
            'posts' => $posts,
139
            'paginator' => $paginator,
140
            'title' => 'Wyniki wyszukiwania dla \''.$term.'\'',
141
            'searchTerm' => $term
142
        ]);
143
    }
144
145
    protected function notFound()
146
    {
147
        $this->provideWidgets();
148
149
        return new Response\HtmlResponse($this->view('not_found.twig'), 404);
150
    }
151
}
152