UserController::usersAction()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.3484

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
rs 6.7272
ccs 21
cts 26
cp 0.8077
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
namespace AdminBundle\Controller;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Doctrine\ORM\QueryBuilder;
25
use DembeloMain\Model\Repository\UserRepositoryInterface;
26
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
27
28
/**
29
 * Class UserController
30
 * @Route(service="app.admin_controller_user")
31
 */
32
class UserController
33
{
34
    /**
35
     * @var UserRepositoryInterface
36
     */
37
    private $userRepository;
38
39
    /**
40
     * @var \Swift_Mailer
41
     */
42
    private $mailer;
43
44
    /**
45
     * @var Templating
46
     */
47
    private $templating;
48
49
    /**
50
     * UserController constructor.
51
     * @param UserRepositoryInterface $userRepository
52
     * @param \Swift_Mailer           $mailer
53
     * @param Templating              $templating
54
     */
55 2
    public function __construct(UserRepositoryInterface $userRepository, \Swift_Mailer $mailer, Templating $templating)
56
    {
57 2
        $this->userRepository = $userRepository;
58 2
        $this->mailer = $mailer;
59 2
        $this->templating = $templating;
60 2
    }
61
62
    /**
63
     * @Route("/users", name="admin_users")
64
     *
65
     * @param Request $request
66
     *
67
     * @return Response
68
     *
69
     * @throws \InvalidArgumentException
70
     */
71 2
    public function usersAction(Request $request): Response
72
    {
73 2
        $filters = $request->query->get('filter');
74
75
        /* @var $query QueryBuilder */
76 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

76
        /** @scrutinizer ignore-call */ 
77
        $query = $this->userRepository->createQueryBuilder();
Loading history...
77 2
        if (null !== $filters) {
78 2
            foreach ($filters as $field => $value) {
79
                if (empty($value) && '0' !== $value) {
80
                    continue;
81
                }
82
                if ('status' === $field) {
83
                    //$value = $value === 'aktiv' ? 1 : 0;
84
                    $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

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