LicenseeController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
dl 0
loc 67
rs 10
ccs 12
cts 22
cp 0.5455

3 Methods

Rating   Name   Duplication   Size   Complexity  
A licenseesAction() 0 14 2
A licenseeSuggestAction() 0 18 2
A __construct() 0 3 1
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace AdminBundle\Controller;
21
22
use DembeloMain\Document\Licensee;
23
use DembeloMain\Model\Repository\LicenseeRepositoryInterface;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
28
/**
29
 * Class LicenseeController
30
 * @Route(service="app.admin_controller_licensee")
31
 */
32
class LicenseeController
33
{
34
    /**
35
     * @var LicenseeRepositoryInterface
36
     */
37
    private $licenseeRepository;
38
39
    /**
40
     * UserController constructor.
41
     * @param LicenseeRepositoryInterface $licenseeRepository
42
     */
43 2
    public function __construct(LicenseeRepositoryInterface $licenseeRepository)
44
    {
45 2
        $this->licenseeRepository = $licenseeRepository;
46 2
    }
47
48
    /**
49
     * @Route("/licensees", name="admin_licensees")
50
     *
51
     * @return Response
52
     *
53
     * @throws \InvalidArgumentException
54
     */
55 2
    public function licenseesAction(): Response
56
    {
57 2
        $licensees = $this->licenseeRepository->findAll();
58
59 2
        $output = array();
60
        /* @var $licensee \DembeloMain\Document\Licensee */
61 2
        foreach ($licensees as $licensee) {
62 1
            $obj = new \stdClass();
63 1
            $obj->id = $licensee->getId();
64 1
            $obj->name = $licensee->getName();
65 1
            $output[] = $obj;
66
        }
67
68 2
        return new Response(\json_encode($output));
69
    }
70
71
    /**
72
     * @Route("/licenseeSuggest", name="admin_licensee_suggest")
73
     *
74
     * @param Request $request
75
     *
76
     * @return Response
77
     *
78
     * @throws \UnexpectedValueException
79
     * @throws \InvalidArgumentException
80
     */
81
    public function licenseeSuggestAction(Request $request): Response
82
    {
83
        $filter = $request->query->get('filter');
84
85
        $searchString = $filter['value'];
86
87
        /* @var $licensees Licensee[] */
88
        $licensees = $this->licenseeRepository->findBy(['name' => new \MongoRegex('/'.$searchString.'/')], null, 10);
89
90
        $output = [];
91
        foreach ($licensees as $licensee) {
92
            $output[] = array(
93
                'id' => $licensee->getId(),
94
                'value' => $licensee->getName(),
95
            );
96
        }
97
98
        return new Response(\json_encode($output));
99
    }
100
}
101