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

UserController::usersAction()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 39
Code Lines 26

Duplication

Lines 11
Ratio 28.21 %

Code Coverage

Tests 21
CRAP Score 7.3484

Importance

Changes 0
Metric Value
dl 11
loc 39
ccs 21
cts 26
cp 0.8077
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 26
nc 4
nop 1
crap 7.3484
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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Doctrine\ORM\QueryBuilder;
27
use DembeloMain\Model\Repository\UserRepositoryInterface;
28
29
/**
30
 * Class UserController
31
 * @Route(service="app.admin_controller_user")
32
 */
33
class UserController extends Controller
34
{
35
    /**
36
     * @var UserRepositoryInterface
37
     */
38
    private $userRepository;
39
40
    /**
41
     * @var \Swift_Mailer
42
     */
43
    private $mailer;
44
45
    /**
46
     * UserController constructor.
47
     * @param UserRepositoryInterface $userRepository
48
     * @param \Swift_Mailer           $mailer
49
     */
50 2
    public function __construct(
51
        UserRepositoryInterface $userRepository,
52
        \Swift_Mailer $mailer
53
    ) {
54 2
        $this->userRepository = $userRepository;
55 2
        $this->mailer = $mailer;
56 2
    }
57
58
    /**
59
     * @Route("/users", name="admin_users")
60
     *
61
     * @param Request $request
62
     * @return Response
63
     * @throws \InvalidArgumentException
64
     */
65 2
    public function usersAction(Request $request): Response
66
    {
67 2
        $filters = $request->query->get('filter');
68
69
        /* @var $query QueryBuilder */
70 2
        $query = $this->userRepository->createQueryBuilder();
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on DembeloMain\Model\Reposi...UserRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to DembeloMain\Model\Reposi...UserRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        /** @scrutinizer ignore-call */ 
71
        $query = $this->userRepository->createQueryBuilder();
Loading history...
71 2
        if (null !== $filters) {
72 2 View Code Duplication
            foreach ($filters as $field => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
                if (empty($value) && $value !== '0') {
74
                    continue;
75
                }
76
                if ($field === 'status') {
77
                    //$value = $value === 'aktiv' ? 1 : 0;
78
                    $query->field($field)->equals((int) $value);
0 ignored issues
show
Bug introduced by
The method field() does not exist on Doctrine\ORM\QueryBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
                    $query->/** @scrutinizer ignore-call */ 
79
                            field($field)->equals((int) $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
                } else {
80
                    $query->field($field)->equals(new \MongoRegex('/.*'.$value.'.*/i'));
81
                }
82
            }
83
        }
84 2
        $users = $query->getQuery()->execute();
85
86 2
        $output = array();
87
        /* @var $user \DembeloMain\Document\User */
88 2
        foreach ($users as $user) {
89 1
            $obj = new \StdClass();
90 1
            $obj->id = $user->getId();
91 1
            $obj->email = $user->getEmail();
92 1
            $obj->roles = implode(', ', $user->getRoles());
93 1
            $obj->licenseeId = $user->getLicenseeId() ?? '';
94 1
            $obj->gender = $user->getGender();
95 1
            $obj->status = $user->getStatus(); // === 0 ? 'inaktiv' : 'aktiv';
96 1
            $obj->source = $user->getSource();
97 1
            $obj->reason = $user->getReason();
98 1
            $obj->created = date('Y-m-d H:i:s', $user->getMetadata()['created']);
99 1
            $obj->updated = date('Y-m-d H:i:s', $user->getMetadata()['updated']);
100 1
            $output[] = $obj;
101
        }
102
103 2
        return new Response(\json_encode($output));
104
    }
105
106
    /**
107
     * @Route("/useractivationmail", name="admin_user_activation_mail")
108
     *
109
     * @param Request $request
110
     * @return Response
111
     * @throws \InvalidArgumentException
112
     */
113
    public function useractivationmailAction(Request $request): Response
114
    {
115
        $userId = $request->request->get('userId');
116
117
        /* @var $user \DembeloMain\Document\User */
118
        $user = $this->userRepository->find($userId);
119
        if (null === $user) {
120
            return new Response(\json_encode(['error' => false]));
121
        }
122
        $user->setActivationHash(sha1($user->getEmail().$user->getPassword().\time()));
123
124
        $this->userRepository->save($user);
125
126
        $message = (new \Swift_Message('waszulesen - Bestätigung der Email-Adresse'))
127
            ->setFrom('[email protected]')
128
            ->setTo($user->getEmail())
129
            ->setBody(
130
                $this->renderView(
131
                    'AdminBundle::Emails/registration.txt.twig',
132
                    array('hash' => $user->getActivationHash())
133
                ),
134
                'text/html'
135
            );
136
137
        $this->mailer->send($message);
138
139
        return new Response(\json_encode(['error' => false]));
140
    }
141
}
142