Completed
Pull Request — 1.x (#637)
by Daniel
14:10 queued 04:01
created

ZfcUserDisplayName::getAuthService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ZfcUser\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
use Zend\Authentication\AuthenticationService;
7
use ZfcUser\Entity\UserInterface as User;
8
9
class ZfcUserDisplayName extends AbstractHelper
10
{
11
    /**
12
     * @var AuthenticationService
13
     */
14
    protected $authService;
15
16
    /**
17
     * __invoke
18
     *
19
     * @access public
20
     * @param \ZfcUser\Entity\UserInterface $user
21
     * @throws \ZfcUser\Exception\DomainException
22
     * @return String
23
     */
24
    public function __invoke(User $user = null)
25
    {
26
        if (null === $user) {
27
            if ($this->getAuthService()->hasIdentity()) {
28
                $user = $this->getAuthService()->getIdentity();
29
                if (!$user instanceof User) {
30
                    throw new \ZfcUser\Exception\DomainException(
31
                        '$user is not an instance of User',
32
                        500
33
                    );
34
                }
35
            } else {
36
                return false;
37
            }
38
        }
39
40
        $displayName = $user->getDisplayName();
41
        if (null === $displayName) {
42
            $displayName = $user->getUsername();
43
        }
44
        // User will always have an email, so we do not have to throw error
45
        if (null === $displayName) {
46
            $displayName = $user->getEmail();
47
            $displayName = substr($displayName, 0, strpos($displayName, '@'));
48
        }
49
50
        return $displayName;
51
    }
52
53
    /**
54
     * Get authService.
55
     *
56
     * @return AuthenticationService
57
     */
58
    public function getAuthService()
59
    {
60
        return $this->authService;
61
    }
62
63
    /**
64
     * Set authService.
65
     *
66
     * @param AuthenticationService $authService
67
     * @return \ZfcUser\View\Helper\ZfcUserDisplayName
68
     */
69
    public function setAuthService(AuthenticationService $authService)
70
    {
71
        $this->authService = $authService;
72
        return $this;
73
    }
74
}
75