Completed
Push — master ( d7040d...7fcf0d )
by Craig
08:15
created

SearchHelper::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Helper;
13
14
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16
use Zikula\PermissionsModule\Api\PermissionApi;
17
use Zikula\SearchModule\Entity\SearchResultEntity;
18
use Zikula\SearchModule\SearchableInterface;
19
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
20
21
class SearchHelper implements SearchableInterface
22
{
23
    /**
24
     * @var PermissionApi
25
     */
26
    private $permissionApi;
27
28
    /**
29
     * @var EngineInterface
30
     */
31
    private $templateEngine;
32
33
    /**
34
     * @var SessionInterface
35
     */
36
    private $session;
37
38
    /**
39
     * @var UserRepositoryInterface
40
     */
41
    private $userRepository;
42
43
    /**
44
     * SearchHelper constructor.
45
     * @param PermissionApi $permissionApi
46
     * @param EngineInterface $templateEngine
47
     * @param SessionInterface $session
48
     * @param UserRepositoryInterface $userRepository
49
     */
50
    public function __construct(
51
        PermissionApi $permissionApi,
52
        EngineInterface $templateEngine,
53
        SessionInterface $session,
54
        UserRepositoryInterface $userRepository
55
    ) {
56
        $this->permissionApi = $permissionApi;
57
        $this->templateEngine = $templateEngine;
58
        $this->session = $session;
59
        $this->userRepository = $userRepository;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getOptions($active, $modVars = null)
66
    {
67
        $options = '';
68
        if ($this->permissionApi->hasPermission('ZikulaUsersModule::', '::', ACCESS_READ)) {
69
            $options = $this->templateEngine->renderResponse('@ZikulaUsersModule/Search/options.html.twig', ['active' => $active])->getContent();
70
        }
71
72
        return $options;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getResults(array $words, $searchType = 'AND', $modVars = null)
79
    {
80
        if (!$this->permissionApi->hasPermission('ZikulaUsersModule::', '::', ACCESS_READ)) {
81
            return [];
82
        }
83
        $users = $this->userRepository->getSearchResults($words);
84
85
        $results = [];
86
        foreach ($users as $user) {
87
            if ($user->getUid() != 1 && $this->permissionApi->hasPermission('ZikulaUsersModule::', $user->getUname() . '::' . $user->getUid(), ACCESS_READ)) {
88
                $result = new SearchResultEntity();
89
                $result->setTitle($user->getUname())
90
                    ->setModule('ZikulaUsersModule')
91
                    ->setCreated($user->getUser_Regdate())
92
                    ->setSesid($this->session->getId());
93
                $results[] = $result;
94
            }
95
        }
96
97
        return $results;
98
    }
99
100
    public function getErrors()
101
    {
102
        return [];
103
    }
104
}
105