Completed
Push — master ( 8065a1...ffd5ae )
by Albert
04:05
created

PostController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 150
Duplicated Lines 24.67 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 5
Metric Value
wmc 8
c 6
b 0
f 5
lcom 1
cbo 14
dl 37
loc 150
ccs 0
cts 75
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 1
A index() 0 12 2
A post() 0 10 2
A category() 12 12 1
A tag() 12 12 1
A search() 13 13 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\CategoryRepositoryInterface;
7
use Albert221\Blog\Repository\PostRepositoryInterface;
8
use Albert221\Blog\Repository\TagRepositoryInterface;
9
use Albert221\Blog\Sidebar\Widget\HTML;
10
use Albert221\Blog\Sidebar\Widget\RecentCategories;
11
use Albert221\Blog\Sidebar\Widget\RecentPosts;
12
use Albert221\Blog\Sidebar\Widget\TagCloud;
13
use Albert221\Blog\Sidebar\WidgetManager;
14
use League\Route\Http\Exception\NotFoundException;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Twig_Environment;
17
use Zend\Diactoros\Request;
18
19
class PostController extends AbstractController
20
{
21
    /**
22
     * @var PostRepositoryInterface Posts
23
     */
24
    protected $posts;
25
26
    /**
27
     * @var CategoryRepositoryInterface Categories
28
     */
29
    protected $categories;
30
31
    /**
32
     * @var PaginatorBuilder Paginator builder
33
     */
34
    protected $paginatorBuilder;
35
36
    public function __construct(
37
        PostRepositoryInterface $posts,
38
        CategoryRepositoryInterface $categories,
39
        TagRepositoryInterface $tags,
40
        PaginatorBuilder $paginatorBuilder,
41
        Twig_Environment $twig
42
    ) {
43
        parent::__construct($twig);
44
        
45
        $this->posts = $posts;
46
        $this->categories = $categories;
47
        $this->paginatorBuilder = $paginatorBuilder;
48
49
        // TODO: Find a better place for everything below.
50
51
        $sidebarWidgetManager = new WidgetManager();
52
        $sidebarWidgetManager->add(new RecentPosts($this->posts, $twig, 10));
53
        $sidebarWidgetManager->add(new TagCloud($tags, $twig));
54
55
        $footerWidgetManager = new WidgetManager();
56
        $footerWidgetManager->add(new RecentPosts($this->posts, $twig, 5));
57
        $footerWidgetManager->add(new RecentCategories($this->categories, $twig, 5));
58
        $footerWidgetManager->add(new HTML('Polecane strony', '<ul>
59
            <li><a href="#">Lorem ipsum.</a></li>
60
            <li><a href="#">Amet, ipsam?</a></li>
61
            <li><a href="#">Animi, alias.</a></li>
62
            <li><a href="#">Sed, non.</a></li>
63
            <li><a href="#">Officiis, harum.</a></li>
64
        </ul>'));
65
66
        $this->twig->addGlobal('sidebarWidgets', $sidebarWidgetManager->getWidgets());
67
        $this->twig->addGlobal('footerWidgets', $footerWidgetManager->getWidgets());
68
    }
69
70
    /**
71
     * /
72
     *
73
     * @param  ServerRequestInterface $request
74
     *
75
     * @return string
76
     */
77
    public function index(ServerRequestInterface $request)
78
    {
79
        if (isset($request->getQueryParams()['q'])) {
80
            return $this->search($request, $request->getQueryParams()['q']);
81
        }
82
83
        $paginator = $this->paginatorBuilder->build($request, $this->posts->count());
84
85
        $posts = $this->posts->paginated($paginator->getPage(), $paginator->getPerPage());
86
87
        return $this->view('index.twig', compact('posts', 'paginator'));
88
    }
89
90
    /**
91
     * /{slug}
92
     *
93
     * @param string $slug
94
     * @return string
95
     * @throws NotFoundException when post does not exist
96
     */
97
    public function post($slug)
98
    {
99
        $post = $this->posts->bySlug($slug);
100
101
        if (!$post) {
102
            throw new NotFoundException('Post has not been found');
103
        }
104
        
105
        return $this->view('post.twig', compact('post'));
106
    }
107
108
    /**
109
     * /kategoria/{slug}
110
     *
111
     * @param ServerRequestInterface $request
112
     * @param string $slug
113
     * @return string
114
     */
115 View Code Duplication
    public function category(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...
116
    {
117
        $paginator = $this->paginatorBuilder->build($request, $this->posts->byCategoryCount($slug));
118
119
        $posts = $this->posts->byCategory($slug, $paginator->getPage(), $paginator->getPerPage());
120
121
        return $this->view('index.twig', [
122
            'posts' => $posts,
123
            'paginator' => $paginator,
124
            'title' => 'Posty w kategorii \''.$posts[0]->getCategory()->getName().'\''
125
        ]);
126
    }
127
128
    /**
129
     * /tag/{slug}
130
     *
131
     * @param ServerRequestInterface $request
132
     * @param string $slug
133
     * @return string
134
     */
135 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...
136
    {
137
        $paginator = $this->paginatorBuilder->build($request, $this->posts->byTagCount($slug));
138
139
        $posts = $this->posts->byTag($slug, $paginator->getPage(), $paginator->getPerPage());
140
141
        return $this->view('index.twig', [
142
            'posts' => $posts,
143
            'paginator' => $paginator,
144
            'title' => 'Posty otagowane \''.$slug.'\''
145
        ]);
146
    }
147
148
    /**
149
     * /?q={term}
150
     *
151
     * @param ServerRequestInterface $request
152
     * @param string $term
153
     * @return string
154
     */
155 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...
156
    {
157
        $paginator = $this->paginatorBuilder->build($request, $this->posts->searchCount($term));
158
159
        $posts = $this->posts->search($term, $paginator->getPage(), $paginator->getPerPage());
160
161
        return $this->view('index.twig', [
162
            'posts' => $posts,
163
            'paginator' => $paginator,
164
            'title' => 'Wyniki wyszukiwania dla \''.$term.'\'',
165
            'searchTerm' => $term
166
        ]);
167
    }
168
}
169