Completed
Push — master ( d8e3c1...511324 )
by Konstantinos
04:17
created

SearchController::playerByBzidAction()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 12
nc 6
nop 4
1
<?php
2
3
use Symfony\Component\HttpFoundation\JsonResponse;
4
use Symfony\Component\HttpFoundation\RedirectResponse;
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
7
8
class SearchController extends JSONController
9
{
10
    /**
11
     * The maximum number of objects to allow being excluded
12
     *
13
     * @var int
14
     */
15
    const MAX_EXCLUDED_OBJECTS = 10;
16
17
    /**
18
     * A list of types that can be returned when searching
19
     *
20
     * @var array
21
     */
22
    private static $acceptableTypes = array('player', 'team');
23
24
    public function searchAction(Request $request, Player $me)
0 ignored issues
show
Unused Code introduced by
The parameter $me 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...
25
    {
26
        if (!$request->query->has('types')) {
27
            throw new \BadRequestException();
28
        }
29
30
        $types = explode(',', $request->query->get('types'));
31
        $excludeQuery = $request->query->get('exclude');
32
33
        $queries = $results = array();
34
35
        foreach ($types as &$type) {
36
            if (!in_array($type, self::$acceptableTypes)) {
37
                throw new \BadRequestException("An invalid type was provided");
38
            }
39
40
            $type = ucfirst($type);
41
            $queries[$type] = $type::getQueryBuilder();
42
        }
43
44
        $excluded = $this->decompose($excludeQuery, $types, false, self::MAX_EXCLUDED_OBJECTS);
45
46
        foreach ($queries as $type => $query) {
47
            if ($startsWith = $request->query->get('startsWith')) {
48
                $query->where('name')->startsWith($startsWith);
49
            }
50
51
            $query->active()->sortBy('name');
52
53
            foreach ($excluded[$type] as $exclude) {
54
                $query->except($exclude);
55
            }
56
57
            if ($type === 'Player') {
58
                $result = $this->playerQuery($query, $request);
59
            } else {
60
                $result = $query->getArray(array('name'));
61
            }
62
63
            foreach ($result as $model) {
64
                $model['type'] = $type;
65
                $results[] = $model;
66
            }
67
        }
68
69
        return new JsonResponse(array(
70
            'results' => $results
71
        ));
72
    }
73
74
    private function playerQuery(\QueryBuilder $query, Request $request)
75
    {
76
        if ($team = $request->query->get('team')) {
77
            $query->where('team')->is($team);
78
        }
79
80
        return $query->getArray(array('name', 'outdated'));
81
    }
82
83
    public function playerByBzidAction(Player $me, Request $request, FlashBag $flashBag, $bzid = null)
84
    {
85
        if (!$me->hasPermission(Permission::VIEW_VISITOR_LOG)) {
86
            throw new ForbiddenException();
87
        }
88
89
        if ($bzid === null) {
90
            if (!$request->query->has('bzid')) {
91
                throw new BadRequestException("Please provide the BZID to search for");
92
            }
93
94
            $bzid = $request->query->get('bzid');
95
        }
96
97
        $player = Player::getFromBZID($bzid);
98
99
        if (!$player->isValid()) {
100
            $flashBag->add('error', "Player with BZID $bzid not found");
101
102
            return $this->goBack();
103
        }
104
105
        return new RedirectResponse($player->getURL());
106
    }
107
}
108