Completed
Push — master ( 12d04d...f0be6a )
by Albert
02:59
created

PostController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 113
Duplicated Lines 23.89 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 5
c 3
b 0
f 2
lcom 1
cbo 10
dl 27
loc 113
ccs 0
cts 53
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 1
A index() 11 11 1
A post() 0 6 1
A category() 8 8 1
A tag() 8 8 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 Psr\Http\Message\ServerRequestInterface;
15
use Twig_Environment;
16
use Zend\Diactoros\Request;
17
18
class PostController extends AbstractController
19
{
20
    /**
21
     * @var PostRepositoryInterface Posts
22
     */
23
    protected $posts;
24
25
    /**
26
     * @var CategoryRepositoryInterface Categories
27
     */
28
    protected $categories;
29
30
    /**
31
     * @var PaginatorBuilder Paginator builder
32
     */
33
    protected $paginatorBuilder;
34
35
    public function __construct(
36
        PostRepositoryInterface $posts,
37
        CategoryRepositoryInterface $categories,
38
        TagRepositoryInterface $tags,
39
        PaginatorBuilder $paginatorBuilder,
40
        Twig_Environment $twig
41
    ) {
42
        parent::__construct($twig);
43
        
44
        $this->posts = $posts;
45
        $this->categories = $categories;
46
        $this->paginatorBuilder = $paginatorBuilder;
47
48
        // TODO: Find a better place for everything below.
49
50
        $sidebarWidgetManager = new WidgetManager();
51
        $sidebarWidgetManager->add(new RecentPosts($this->posts, $twig, 10));
52
        $sidebarWidgetManager->add(new TagCloud($tags, $twig));
53
54
        $footerWidgetManager = new WidgetManager();
55
        $footerWidgetManager->add(new RecentPosts($this->posts, $twig, 5));
56
        $footerWidgetManager->add(new RecentCategories($this->categories, $twig, 5));
57
        $footerWidgetManager->add(new HTML('Polecane strony', '<ul>
58
            <li><a href="#">Lorem ipsum.</a></li>
59
            <li><a href="#">Amet, ipsam?</a></li>
60
            <li><a href="#">Animi, alias.</a></li>
61
            <li><a href="#">Sed, non.</a></li>
62
            <li><a href="#">Officiis, harum.</a></li>
63
        </ul>'));
64
65
        $this->twig->addGlobal('sidebarWidgets', $sidebarWidgetManager->getWidgets());
66
        $this->twig->addGlobal('footerWidgets', $footerWidgetManager->getWidgets());
67
    }
68
69
    /**
70
     * Route: /
71
     *
72
     * @param  ServerRequestInterface $request
73
     *
74
     * @return string
75
     */
76 View Code Duplication
    public function index(ServerRequestInterface $request)
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...
77
    {
78
        $paginator = $this->paginatorBuilder->build($request, $this->posts->count());
79
80
        $posts = $this->posts->paginated($paginator->getPage(), $paginator->getPerPage());
81
82
        return $this->view('index.twig', [
83
            'posts' => $posts,
84
            'paginator' => $paginator
85
        ]);
86
    }
87
88
    /**
89
     * Route: /{slug}
90
     *
91
     * @param string $slug
92
     * @return string
93
     */
94
    public function post($slug)
95
    {
96
        $post = $this->posts->bySlug($slug);
97
        
98
        return $this->view('post.twig', compact('post'));
99
    }
100
101
    /**
102
     * Route: /kategoria/{slug}
103
     *
104
     * @param ServerRequestInterface $request
105
     * @param string $slug
106
     * @return string
107
     */
108 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...
109
    {
110
        $paginator = $this->paginatorBuilder->build($request, $this->posts->byCategoryCount($slug));
111
112
        $posts = $this->posts->byCategory($slug, $paginator->getPage(), $paginator->getPerPage());
113
114
        return $this->view('index.twig', compact('posts', 'paginator'));
115
    }
116
117
    /**
118
     * @param ServerRequestInterface $request
119
     * @param string $slug
120
     * @return string
121
     */
122 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...
123
    {
124
        $paginator = $this->paginatorBuilder->build($request, $this->posts->byTagCount($slug));
125
126
        $posts = $this->posts->byTag($slug, $paginator->getPage(), $paginator->getPerPage());
127
128
        return $this->view('index.twig', compact('posts', 'paginator'));
129
    }
130
}
131