ListController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 4
c 7
b 1
f 3
lcom 1
cbo 5
dl 0
loc 77
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B onDispatch() 0 28 3
1
<?php
2
3
namespace Sebaks\Crud\Controller;
4
5
use Zend\Mvc\MvcEvent;
6
use Zend\Mvc\Controller\AbstractActionController;
7
use Zend\InputFilter\InputFilterInterface;
8
use T4webDomainInterface\Infrastructure\RepositoryInterface;
9
use Sebaks\Crud\View\Model\ListViewModelInterface;
10
11
class ListController extends AbstractActionController
12
{
13
    /**
14
     * @var array
15
     */
16
    private $query;
17
18
    /**
19
     * @var InputFilterInterface
20
     */
21
    private $inputFilter;
22
23
    /**
24
     * @var RepositoryInterface
25
     */
26
    private $repository;
27
28
    /**
29
     * @var ListViewModelInterface
30
     */
31
    private $viewModel;
32
33
    /**
34
     * ListController constructor.
35
     * @param array $query
36
     * @param InputFilterInterface $inputFilter
37
     * @param RepositoryInterface $repository
38
     * @param ListViewModelInterface $viewModel
39
     */
40
    public function __construct(
41
        array $query,
42
        RepositoryInterface $repository,
43
        ListViewModelInterface $viewModel,
44
        InputFilterInterface $inputFilter = null
45
    )
46
    {
47
        $this->query = $query;
48
        $this->repository = $repository;
49
        $this->viewModel = $viewModel;
50
        $this->inputFilter = $inputFilter;
51
    }
52
53
    /**
54
     * Execute the request
55
     *
56
     * @param  MvcEvent $e
57
     * @return ListViewModelInterface
58
     */
59
    public function onDispatch(MvcEvent $e)
60
    {
61
        if ($this->inputFilter) {
62
            $this->inputFilter->setData($this->query);
63
            if ($this->inputFilter->isValid()) {
64
                $validData = $this->inputFilter->getValues();
65
66
                $criteria = $this->repository->createCriteria($validData);
67
                $collection = $this->repository->findMany($criteria);
68
69
                $this->viewModel->setCollection($collection);
0 ignored issues
show
Documentation introduced by
$collection is of type array<integer,object<T4w...rface\EntityInterface>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
                $this->viewModel->setInputData($validData);
71
            } else {
72
                $this->viewModel->setErrors($this->inputFilter->getMessages());
73
                $this->viewModel->setInputData($this->query);
74
            }
75
        } else {
76
            $criteria = $this->repository->createCriteria($this->query);
77
            $collection = $this->repository->findMany($criteria);
78
79
            $this->viewModel->setCollection($collection);
0 ignored issues
show
Documentation introduced by
$collection is of type array<integer,object<T4w...rface\EntityInterface>>, but the function expects a object<ArrayObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
            $this->viewModel->setInputData($this->query);
81
        }
82
83
        $e->setResult($this->viewModel);
84
85
        return $this->viewModel;
86
    }
87
}
88