Completed
Pull Request — master (#21)
by Daniel
02:36
created

Grid   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 9.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 6
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createView() 0 8 1
A performActionFromPostData() 6 19 2
A performAction() 0 9 1
A __construct() 0 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 Psi\Component\Grid;
4
5
use Psi\Component\Grid\Cell\View\SelectView;
6
use Psi\Component\Grid\Metadata\GridMetadata;
7
use Psi\Component\Grid\View\ActionBar;
8
use Psi\Component\ObjectAgent\AgentInterface;
9
use Psi\Component\ObjectAgent\Query\Query;
10
11
class Grid
12
{
13
    private $agent;
14
    private $gridContext;
15
    private $gridMetadata;
16
    private $gridViewFactory;
17
    private $actionPerformer;
18
19
    public function __construct(
20
        GridViewFactory $gridViewFactory,
21
        ActionPerformer $actionPerformer,
22
        AgentInterface $agent,
23
        GridContext $gridContext,
24
        GridMetadata $gridMetadata
25
    ) {
26
        $this->agent = $agent;
27
        $this->gridContext = $gridContext;
28
        $this->gridMetadata = $gridMetadata;
29
        $this->gridViewFactory = $gridViewFactory;
30
        $this->actionPerformer = $actionPerformer;
31
    }
32
33
    public function createView()
34
    {
35
        return $this->gridViewFactory->createView(
36
            $this->agent,
37
            $this->gridContext,
38
            $this->gridMetadata
39
        );
40
    }
41
42
    public function performActionFromPostData(array $postData)
43
    {
44
        $required = [
45
            ActionBar::INPUT_NAME,
46
            SelectView::INPUT_NAME,
47
        ];
48
49 View Code Duplication
        if (array_diff($required, array_keys($postData))) {
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...
50
            throw new \InvalidArgumentException(sprintf(
51
                'Expected all keys "%s" in post data, but (only) got "%s"',
52
                implode('", "', $required), implode('", "', array_keys($postData))
53
            ));
54
        }
55
56
        $actionName = $postData[ActionBar::INPUT_NAME];
57
        $selectedIdentifiers = array_keys($postData[SelectView::INPUT_NAME]);
58
59
        $this->performAction($actionName, $selectedIdentifiers);
60
    }
61
62
    public function performAction(string $actionName, array $selectedIdentifiers)
63
    {
64
        $this->actionPerformer->perform(
65
            $this->agent,
66
            $this->gridMetadata,
67
            $actionName,
68
            $selectedIdentifiers
69
        );
70
    }
71
}
72