Failed Conditions
Push — master ( 4252f4...9d8e13 )
by Adrien
07:13
created

RequestPasswordReset::build()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 1
nop 0
dl 0
loc 41
rs 9.2408
c 0
b 0
f 0
ccs 21
cts 21
cp 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Field\FieldInterface;
8
use Application\Api\Scalar\LoginType;
9
use Application\DBAL\Types\RelationshipType;
10
use Application\Model\User;
11
use Application\Repository\UserRepository;
12
use Application\Service\Mailer;
13
use GraphQL\Type\Definition\Type;
14
use Zend\Expressive\Session\SessionInterface;
15
16
abstract class RequestPasswordReset implements FieldInterface
17
{
18 3
    public static function build(): array
19
    {
20
        return [
21 1
            'name' => 'requestPasswordReset',
22 1
            'type' => Type::nonNull(_types()->get('Relationship')),
23 1
            'description' => 'Request to send an email to reset the password for the given user. It will **always** return a successful response, even if the user is not found.',
24
            'args' => [
25 1
                'login' => Type::nonNull(_types()->get(LoginType::class)),
26
            ],
27
            'resolve' => function ($root, array $args, SessionInterface $session): string {
1 ignored issue
show
Unused Code introduced by
The parameter $session is not used and could be removed. ( Ignorable by Annotation )

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

27
            'resolve' => function ($root, array $args, /** @scrutinizer ignore-unused */ SessionInterface $session): string {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28 3
                global $container;
29
                /** @var Mailer $mailer */
30 3
                $mailer = $container->get(Mailer::class);
31
32
                /** @var UserRepository $repository */
33 3
                $repository = _em()->getRepository(User::class);
34
35
                /** @var User $user */
36 3
                $user = $repository->getByLogin($args['login']);
37 3
                $relationship = RelationshipType::HOUSEHOLDER;
38
39 3
                if ($user) {
40 2
                    $email = $user->getEmail();
41
42
                    // Fallback to householder if any
43 2
                    if (!$email && $user->getOwner()) {
44 1
                        $repository->getAclFilter()->setEnabled(false);
45 1
                        $email = $user->getOwner()->getEmail();
46 1
                        $repository->getAclFilter()->setEnabled(true);
47
48 1
                        $relationship = $user->getFamilyRelationship();
49
                    }
50
51 2
                    if ($email) {
52 2
                        $message = $mailer->queueResetPassword($user, $email);
53 2
                        $mailer->sendMessageAsync($message);
54
                    }
55
                }
56
57
                // Here we lie to client, and always say we are successful, to avoid data leak
58 3
                return $relationship;
59 1
            },
60
        ];
61
    }
62
}
63