Completed
Pull Request — master (#452)
by
unknown
03:17
created

resolveCreatedByToUserId()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.4468
c 0
b 0
f 0
cc 6
nc 17
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CultuurNet\UDB3\UiTID;
4
5
use CultuurNet\UDB3\Cdb\CreatedByToUserIdResolverInterface;
6
use CultuurNet\UDB3\User\UserIdentityDetails;
7
use CultuurNet\UDB3\User\UserIdentityResolverInterface;
8
use Exception;
9
use Psr\Log\LoggerAwareInterface;
10
use Psr\Log\LoggerAwareTrait;
11
use Psr\Log\NullLogger;
12
use ValueObjects\Exception\InvalidNativeArgumentException;
13
use ValueObjects\Identity\UUID;
14
use ValueObjects\StringLiteral\StringLiteral;
15
use ValueObjects\Web\EmailAddress;
16
17
class CdbXmlCreatedByToUserIdResolver implements LoggerAwareInterface, CreatedByToUserIdResolverInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /**
22
     * @var UserIdentityResolverInterface
23
     */
24
    private $users;
25
26
    public function __construct(UserIdentityResolverInterface $users)
27
    {
28
        $this->users = $users;
29
        $this->logger = new NullLogger();
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function resolveCreatedByToUserId(StringLiteral $createdByIdentifier): ?StringLiteral
36
    {
37
        try {
38
            // If the createdby is a UUID, return it immediately.
39
            UUID::fromNative($createdByIdentifier->toNative());
40
            return $createdByIdentifier;
41
        } catch (InvalidNativeArgumentException $exception) {
42
            $this->logger->info(
43
                'The provided createdByIdentifier ' . $createdByIdentifier->toNative() . ' is not a UUID.',
44
                [
45
                    'exception' => $exception,
46
                ]
47
            );
48
        }
49
50
        try {
51
            // If the createdby is not a UUID, it might still be an Auth0 or social id.
52
            $user = $this->users->getUserById($createdByIdentifier);
53
            if ($user instanceof UserIdentityDetails) {
54
                return $user->getUserId();
55
            }
56
57
            // If no user was found with the createdby as id, check if it's an email and look up the user that way.
58
            // Otherwise look it up as a username.
59
            try {
60
                $email = new EmailAddress($createdByIdentifier->toNative());
61
                $user = $this->users->getUserByEmail($email);
62
            } catch (InvalidNativeArgumentException $e) {
63
                $user = $this->users->getUserByNick($createdByIdentifier);
64
            }
65
            if ($user instanceof UserIdentityDetails) {
66
                return $user->getUserId();
67
            }
68
        } catch (Exception $e) {
69
            $this->logger->error(
70
                sprintf(
71
                    'An unexpected error occurred while resolving user with identifier %s',
72
                    $createdByIdentifier
73
                ),
74
                [
75
                    'exception' => $e,
76
                ]
77
            );
78
        }
79
80
        $this->logger->warning(
81
            'Unable to find user with identifier ' . $createdByIdentifier
82
        );
83
84
        return null;
85
    }
86
}
87