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

ZfcUserDisplayName   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 3 Features 0
Metric Value
wmc 8
c 3
b 3
f 0
lcom 0
cbo 4
dl 0
loc 66
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthService() 0 4 1
B __invoke() 0 28 6
A setAuthService() 0 5 1
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