Completed
Push — master ( 5cd95a...6f73c5 )
by Oscar
03:28
created

SearchEntity   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 92
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C html() 0 50 8
A json() 0 13 2
A search() 0 16 4
1
<?php
2
3
namespace Folk\Controllers;
4
5
use Psr\Http\Message\ServerRequestInterface as Request;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Folk\Admin;
8
use Folk\Entities\EntityInterface;
9
use Zend\Diactoros\Response\RedirectResponse;
10
use Folk\SearchQuery;
11
12
class SearchEntity extends Entity
13
{
14
    public function html(Request $request, Response $response, Admin $app, EntityInterface $entity)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
15
    {
16
        $search = new SearchQuery($request->getQueryParams());
17
        $items = $this->search($entity, $search);
18
19
        //Redirect to edit element if it's only one result
20
        if (count($items) === 1 && !empty($search->getQuery())) {
21
            return new RedirectResponse($app->getRoute('read', [
22
                'entity' => $entity->getName(),
23
                'id' => key($items),
24
            ]));
25
        }
26
27
        //Load the values
28
        $row = $entity->getScheme($app['builder']);
29
30
        //Remove non-listable elements
31
        $removedKeys = [];
32
33
        foreach ($row as $key => $value) {
34
            if ($value->get('list') === false) {
35
                $removedKeys[] = $key;
36
            }
37
        }
38
39
        foreach ($removedKeys as $key) {
40
            unset($row[$key]);
41
        }
42
43
        //populate the data
44
        $rows = [];
45
46
        foreach ($items as $id => &$item) {
47
            $rows[$id] = clone $row;
48
            $rows[$id]->val($item);
49
        }
50
51
        if (count($items) === 50) {
52
            $search->setPage($search->getPage() + 1);
53
        } else {
54
            $search->setPage(null);
55
        }
56
57
        //List all results
58
        return $app['templates']->render('pages/search', [
59
            'rows' => $rows,
60
            'entity' => $entity,
61
            'search' => $search,
62
        ]);
63
    }
64
65
    public function json(Request $request, Response $response, Admin $app)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $entity = $app->getEntity($request->getAttribute('entity'));
68
        $search = new SearchQuery($request->getQueryParams());
69
70
        $json = [];
71
72
        foreach ($this->search($entity, $search) as $id => $item) {
0 ignored issues
show
Bug introduced by
It seems like $entity defined by $app->getEntity($request->getAttribute('entity')) on line 67 can be null; however, Folk\Controllers\SearchEntity::search() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
73
            $json[$id] = $entity->getLabel($id, $item);
74
        }
75
76
        return json_encode($json);
77
    }
78
79
    /**
80
     * Execute the search in the entity.
81
     * 
82
     * @param EntityInterface $entity
83
     * @param SearchQuery     $search
84
     * 
85
     * @return array
86
     */
87
    private function search(EntityInterface $entity, SearchQuery $search)
88
    {
89
        if ($search->getPage() === null) {
90
            $search->setPage(1);
91
        }
92
93
        if (($id = $search->getId())) {
94
            if ($items = $entity->read($id)) {
95
                return [$id => $items];
96
            }
97
98
            return [];
99
        }
100
101
        return $entity->search($search);
102
    }
103
}
104