Completed
Push — master ( b9c485...94fed1 )
by Michał
13:51
created

src/Sylius/Component/Core/Model/ShopUser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Sylius\Component\Customer\Model\CustomerInterface as BaseCustomerInterface;
16
use Sylius\Component\Rbac\Model\RoleInterface;
17
use Sylius\Component\User\Model\User as BaseUser;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 * @author Michał Marcinkowski <[email protected]>
22
 */
23
class ShopUser extends BaseUser implements ShopUserInterface
24
{
25
    /**
26
     * @var ArrayCollection
27
     */
28
    protected $authorizationRoles;
29
30
    /**
31
     * @var CustomerInterface
32
     */
33
    protected $customer;
34
35
    public function __construct()
36
    {
37
        parent::__construct();
38
        $this->authorizationRoles = new ArrayCollection();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getAuthorizationRoles()
45
    {
46
        return $this->authorizationRoles;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->authorizationRoles; (Doctrine\Common\Collections\ArrayCollection) is incompatible with the return type declared by the interface Sylius\Component\Rbac\Mo...::getAuthorizationRoles of type Sylius\Component\Rbac\Model\RoleInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function addAuthorizationRole(RoleInterface $role)
53
    {
54
        if (!$this->hasAuthorizationRole($role)) {
55
            $this->authorizationRoles->add($role);
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function removeAuthorizationRole(RoleInterface $role)
63
    {
64
        if ($this->hasAuthorizationRole($role)) {
65
            $this->authorizationRoles->removeElement($role);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function hasAuthorizationRole(RoleInterface $role)
73
    {
74
        return $this->authorizationRoles->contains($role);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getRoles()
81
    {
82
        $roles = parent::getRoles();
83
84
        foreach ($this->getAuthorizationRoles() as $role) {
85
            $roles = array_merge($roles, $role->getSecurityRoles());
86
        }
87
88
        return $roles;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getCustomer()
95
    {
96
        return $this->customer;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function setCustomer(BaseCustomerInterface $customer = null)
103
    {
104
        if ($this->customer !== $customer) {
105
            $this->customer = $customer;
106
            $this->assignUser($customer);
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getEmail()
114
    {
115
        return $this->customer->getEmail();
116
    }
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function setEmail($email)
121
    {
122
        $this->customer->setEmail($email);
123
    }
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getEmailCanonical()
128
    {
129
        return $this->customer->getEmailCanonical();
130
    }
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function setEmailCanonical($emailCanonical)
135
    {
136
        $this->customer->setEmailCanonical($emailCanonical);
137
    }
138
139
    /**
140
     * @param CustomerInterface $customer
141
     */
142
    protected function assignUser(CustomerInterface $customer = null)
143
    {
144
        if (null !== $customer) {
145
            $customer->setUser($this);
146
        }
147
    }
148
}
149