Completed
Push — master ( 693283...8cba26 )
by Craig
06:26
created

LiveSearchController::getUsersAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
cc 4
eloc 22
nc 3
nop 1
rs 8.5806
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\UsersModule\Controller;
13
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Zikula\Core\Controller\AbstractController;
19
use Zikula\UsersModule\Constant as UsersConstant;
20
21
/**
22
 * @Route("/livesearch")
23
 */
24
class LiveSearchController extends AbstractController
25
{
26
    /**
27
     * Retrieves a list of users for a given search term (fragment).
28
     *
29
     * @Route("/getUsers", options={"expose"=true})
30
     * @Method("GET")
31
     *
32
     * @param Request $request Current request instance
33
     *
34
     * @return JsonResponse
35
     */
36
    public function getUsersAction(Request $request)
37
    {
38
        if (!$this->hasPermission('ZikulaUsersModule::LiveSearch', '::', ACCESS_EDIT)) {
39
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by Zikula\UsersModule\Contr...troller::getUsersAction of type Symfony\Component\HttpFoundation\JsonResponse.

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...
40
        }
41
42
        $fragment = $request->query->get('fragment', '');
43
        $userRepository = $this->get('zikula_users_module.user_repository');
44
        $limit = 50;
45
        $filter = [
46
            'activated' => ['operator' => 'notIn', 'operand' => [
47
                UsersConstant::ACTIVATED_PENDING_REG,
48
                UsersConstant::ACTIVATED_PENDING_DELETE
49
            ]],
50
            'uname' => ['operator' => 'like', 'operand' => '%' . $fragment . '%']
51
        ];
52
        $results = $userRepository->query($filter, ['uname' => 'asc'], $limit);
53
54
        // load avatar plugin
55
        // @todo fix this as part of https://github.com/zikula-modules/Profile/issues/80
56
        include_once 'lib/legacy/viewplugins/function.useravatar.php';
57
        $view = \Zikula_View::getInstance('ZikulaUsersModule', false);
58
59
        $resultItems = [];
60
        if (count($results) > 0) {
61
            foreach ($results as $result) {
62
                $resultItems[] = [
63
                    'uid' => $result->getUid(),
64
                    'uname' => $result->getUname(),
65
                    'avatar' => smarty_function_useravatar(['uid' => $result->getUid(), 'rating' => 'g'], $view)
66
                ];
67
            }
68
        }
69
70
        // return response
71
        return new JsonResponse($resultItems);
72
    }
73
}
74