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

Grid::getNumberOfRecords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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 $actionPerformer;
17
18
    public function __construct(
19
        GridViewFactory $gridViewFactory,
20
        ActionPerformer $actionPerformer,
21
        AgentInterface $agent,
22
        GridContext $gridContext,
23
        GridMetadata $gridMetadata
24
    ) {
25
        $this->agent = $agent;
26
        $this->gridContext = $gridContext;
27
        $this->gridMetadata = $gridMetadata;
28
        $this->gridViewFactory = $gridViewFactory;
0 ignored issues
show
Bug introduced by
The property gridViewFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
        $this->actionPerformer = $actionPerformer;
30
    }
31
32
    public function createView()
33
    {
34
        return $this->gridViewFactory->createView(
35
            $this->agent,
36
            $this->gridContext,
37
            $this->gridMetadata
38
        );
39
    }
40
41
    public function performActionFromPostData(array $postData)
42
    {
43
        $required = [
44
            ActionBar::INPUT_NAME,
45
            SelectView::INPUT_NAME,
46
        ];
47
48 View Code Duplication
        if ($diff = array_diff($required, array_keys($postData))) {
0 ignored issues
show
Unused Code introduced by
$diff is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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...
49
            throw new \InvalidArgumentException(sprintf(
50
                'Expected all keys "%s" in post data, but (only) got "%s"',
51
                implode('", "', $required), implode('", "', array_keys($postData))
52
            ));
53
        }
54
55
        $actionName = $postData[ActionBar::INPUT_NAME];
56
        $selectedIdentifiers = array_keys($postData[SelectView::INPUT_NAME]);
57
58
        $this->performAction($actionName, $selectedIdentifiers);
59
    }
60
61
    public function performAction(string $actionName, array $selectedIdentifiers)
62
    {
63
        $this->actionPerformer->perform(
64
            $this->agent,
65
            $this->gridMetadata,
66
            $actionName,
67
            $selectedIdentifiers
68
        );
69
    }
70
71
    private function getNumberOfRecords(Query $query)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
72
    {
73
        if (false === $this->agent->getCapabilities()->canQueryCount()) {
74
            return;
75
        }
76
77
        return $this->agent->queryCount($query);
78
    }
79
}
80