Completed
Pull Request — master (#137)
by Kristof
04:49
created

resolveByEmailOrByNick()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 3
nop 1
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\UiTID;
7
8
use CultuurNet\UDB3\Cdb\CreatedByToUserIdResolverInterface;
9
use Exception;
10
use Psr\Log\LoggerAwareInterface;
11
use Psr\Log\LoggerAwareTrait;
12
use Psr\Log\NullLogger;
13
use ValueObjects\Exception\InvalidNativeArgumentException;
14
use ValueObjects\String\String;
15
use ValueObjects\Web\EmailAddress;
16
17
class CdbXmlCreatedByToUserIdResolver implements LoggerAwareInterface, CreatedByToUserIdResolverInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /**
22
     * @var UsersInterface
23
     */
24
    private $users;
25
26
    /**
27
     * @param UsersInterface $users
28
     */
29
    public function __construct(UsersInterface $users)
30
    {
31
        $this->users = $users;
32
        $this->logger = new NullLogger();
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function resolveCreatedByToUserId(String $createdByIdentifier)
39
    {
40
        $userId = null;
41
42
        try {
43
            $userId = $this->resolveByEmailOrByNick($createdByIdentifier);
44
45
            if (!$userId) {
46
                $this->logger->warning(
47
                    'Unable to find user with identifier ' . $createdByIdentifier
48
                );
49
            }
50
        } catch (Exception $e) {
51
            $this->logger->error(
52
                sprintf(
53
                    'An unexpected error occurred while resolving user with identifier %s',
54
                    $createdByIdentifier
55
                ),
56
                [
57
                    'exception' => $e,
58
                ]
59
            );
60
        }
61
62
        return $userId;
63
    }
64
65
    /**
66
     * @param String $createdByIdentifier
67
     * @return String
68
     */
69
    private function resolveByEmailOrByNick(String $createdByIdentifier)
70
    {
71
        try {
72
            $email = new EmailAddress($createdByIdentifier->toNative());
0 ignored issues
show
Bug introduced by
It seems like $createdByIdentifier is not always an object, but can also be of type string. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
73
            $userId = $this->users->byEmail($email);
74
        } catch (InvalidNativeArgumentException $e) {
75
            $userId = $this->users->byNick($createdByIdentifier);
76
        }
77
78
        return $userId;
79
    }
80
}
81