Passed
Push — master ( c71cfc...3755d0 )
by Michael
01:44
created

LicenseeController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 27.69 %

Test Coverage

Coverage 54.55%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A licenseesAction() 0 14 2
A licenseeSuggestAction() 18 18 2
A __construct() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
29
/**
30
 * Class LicenseeController
31
 * @Route(service="app.admin_controller_licensee")
32
 */
33
class LicenseeController extends Controller
34
{
35
    /**
36
     * @var LicenseeRepositoryInterface
37
     */
38
    private $licenseeRepository;
39
40
    /**
41
     * UserController constructor.
42
     * @param LicenseeRepositoryInterface $licenseeRepository
43
     */
44 2
    public function __construct(
45
        LicenseeRepositoryInterface $licenseeRepository
46
    ) {
47 2
        $this->licenseeRepository = $licenseeRepository;
48 2
    }
49
50
    /**
51
     * @Route("/licensees", name="admin_licensees")
52
     *
53
     * @return Response
54
     * @throws \InvalidArgumentException
55
     */
56 2
    public function licenseesAction(): Response
57
    {
58 2
        $licensees = $this->licenseeRepository->findAll();
59
60 2
        $output = array();
61
        /* @var $licensee \DembeloMain\Document\Licensee */
62 2
        foreach ($licensees as $licensee) {
63 1
            $obj = new \stdClass();
64 1
            $obj->id = $licensee->getId();
65 1
            $obj->name = $licensee->getName();
66 1
            $output[] = $obj;
67
        }
68
69 2
        return new Response(\json_encode($output));
70
    }
71
72
    /**
73
     * @Route("/licenseeSuggest", name="admin_licensee_suggest")
74
     *
75
     * @param Request $request
76
     * @return Response
77
     * @throws \UnexpectedValueException
78
     * @throws \InvalidArgumentException
79
     */
80 View Code Duplication
    public function licenseeSuggestAction(Request $request): Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $filter = $request->query->get('filter');
83
84
        $searchString = $filter['value'];
85
86
        /* @var $licensees Licensee[] */
87
        $licensees = $this->licenseeRepository->findBy(['name' => new \MongoRegex('/'.$searchString.'/')], null, 10);
88
89
        $output = [];
90
        foreach ($licensees as $licensee) {
91
            $output[] = array(
92
                'id' => $licensee->getId(),
93
                'value' => $licensee->getName(),
94
            );
95
        }
96
97
        return new Response(\json_encode($output));
98
    }
99
}
100