Completed
Push — master ( 437856...5fb154 )
by greg
03:04
created

PrizeCategoryController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 144
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 8
dl 40
loc 144
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getServiceLocator() 0 5 1
A listAction() 16 16 2
A setOptions() 0 6 1
A setPrizeCategoryService() 0 6 1
B addAction() 12 30 3
B editAction() 12 35 3
A getOptions() 0 8 2
A getPrizeCategoryService() 0 10 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 PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Entity\PrizeCategory;
6
use PlaygroundGame\Service\PrizeCategory as PrizeCategoryService;
7
use Zend\Mvc\Controller\AbstractActionController;
8
use Zend\View\Model\ViewModel;
9
use PlaygroundGame\Options\ModuleOptions;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
class PrizeCategoryController extends AbstractActionController
13
{
14
    protected $options;
15
16
    /**
17
     * @var \PlaygroundGame\Service\PrizeCategory
18
     */
19
    protected $prizeCategoryService;
20
21
    /**
22
     *
23
     * @var ServiceManager
24
     */
25
    protected $serviceLocator;
26
27
    public function __construct(ServiceLocatorInterface $locator)
28
    {
29
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $locator of type object<Zend\ServiceManag...erviceLocatorInterface> is incompatible with the declared type object<PlaygroundGame\Co...r\Admin\ServiceManager> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
    }
31
32
    public function getServiceLocator()
33
    {
34
        
35
        return $this->serviceLocator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serviceLocator; (PlaygroundGame\Controller\Admin\ServiceManager) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...ller::getServiceLocator of type Zend\ServiceManager\ServiceLocatorInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
    }
37
    
38 View Code Duplication
    public function listAction()
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...
39
    {
40
        $service = $this->getPrizeCategoryService();
41
        $categories = $service->getPrizeCategoryMapper()->findAll();
42
43
        if (is_array($categories)) {
44
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($categories));
45
        } else {
46
            $paginator = $categories;
47
        }
48
49
        $paginator->setItemCountPerPage(10);
50
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
51
52
        return array('categories' => $paginator);
53
    }
54
55
    public function addAction()
56
    {
57
        $form = $this->getServiceLocator()->get('playgroundgame_prizecategory_form');
58
        $request = $this->getRequest();
59
60
        $category = new PrizeCategory();
61
        $form->bind($category);
62
63 View Code Duplication
        if ($request->isPost()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
            $data = array_merge(
65
                $request->getPost()->toArray(),
66
                $request->getFiles()->toArray()
67
            );
68
            $category = $this->getPrizeCategoryService()->create(
69
                $data,
70
                $category,
71
                'playgroundgame_prizecategory_form'
72
            );
73
            if ($category) {
74
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('la catégorie a été créée');
75
76
                return $this->redirect()->toRoute('admin/playgroundgame/prize-category-list');
77
            }
78
        }
79
80
        $viewModel = new ViewModel();
81
        $viewModel->setTemplate('playground-game/prize-category/prize-category');
82
 
83
        return $viewModel->setVariables(array('form' => $form));
84
    }
85
86
    public function editAction()
87
    {
88
        $prizeCategoryId = $this->getEvent()->getRouteMatch()->getParam('prizeCategoryId');
89
        $category = $this->getPrizeCategoryService()->getPrizeCategoryMapper()->findById($prizeCategoryId);
90
91
        $form = $this->getServiceLocator()->get('playgroundgame_prizecategory_form');
92
93
        $request = $this->getRequest();
94
95
        $form->bind($category);
96
97 View Code Duplication
        if ($request->isPost()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
98
            $data = array_merge(
99
                $request->getPost()->toArray(),
100
                $request->getFiles()->toArray()
101
            );
102
            $category = $this->getPrizeCategoryService()->edit(
103
                $data,
104
                $category,
105
                'playgroundgame_prizecategory_form'
106
            );
107
            if ($category) {
108
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
109
                    'La catégorie a été mise à jour'
110
                );
111
112
                return $this->redirect()->toRoute('admin/playgroundgame/prize-category-list');
113
            }
114
        }
115
116
        $viewModel = new ViewModel();
117
        $viewModel->setTemplate('playground-game/prize-category/prize-category');
118
119
        return $viewModel->setVariables(array('form' => $form));
120
    }
121
122
    public function setOptions(ModuleOptions $options)
123
    {
124
        $this->options = $options;
125
126
        return $this;
127
    }
128
129
    public function getOptions()
130
    {
131
        if (!$this->options instanceof ModuleOptions) {
132
            $this->setOptions($this->getServiceLocator()->get('playgroundgame_module_options'));
133
        }
134
135
        return $this->options;
136
    }
137
138
    public function getPrizeCategoryService()
139
    {
140
        if (!$this->prizeCategoryService) {
141
            $this->prizeCategoryService = $this->getServiceLocator()->get(
142
                'playgroundgame_prizecategory_service'
143
            );
144
        }
145
146
        return $this->prizeCategoryService;
147
    }
148
149
    public function setPrizeCategoryService(PrizeCategoryService $prizeCategoryService)
150
    {
151
        $this->prizeCategoryService = $prizeCategoryService;
152
153
        return $this;
154
    }
155
}
156