Completed
Push — master ( 4a3246...f5d602 )
by WEBEWEB
01:52 queued 10s
created

BadUserRoleException::setRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\CoreBundle\Exception;
13
14
use Symfony\Component\Security\Core\User\UserInterface;
15
use WBW\Bundle\CoreBundle\Model\OriginUrlTrait;
16
use WBW\Bundle\CoreBundle\Model\RedirectUrlTrait;
17
use WBW\Library\Core\Network\HTTP\HTTPInterface;
18
19
/**
20
 * Bad user role exception.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\CoreBundle\Exception
24
 */
25
class BadUserRoleException extends AbstractException {
26
27
    use OriginUrlTrait,
28
        RedirectUrlTrait;
29
30
    /**
31
     * Roles.
32
     *
33
     * @var array
34
     */
35
    private $roles;
36
37
    /**
38
     * User.
39
     *
40
     * @var UserInterface
41
     */
42
    private $user;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param UserInterface $user The user.
48
     * @param array $roles The roles.
49
     * @param string $redirectUrl The redirect.
50
     * @param string $originUrl The route.
51
     */
52
    public function __construct(UserInterface $user, array $roles, $redirectUrl, $originUrl) {
53
        parent::__construct(sprintf("User \"%s\" is not allowed to access to \"%s\" with roles [%s]", $user->getUsername(), $originUrl, implode(",", $roles)), HTTPInterface::HTTP_STATUS_FORBIDDEN);
54
        $this->setOriginUrl($originUrl);
55
        $this->setRedirectUrl($redirectUrl);
56
        $this->setRoles($roles);
57
        $this->setUser($user);
58
    }
59
60
    /**
61
     * Get the roles.
62
     *
63
     * @return array Returns the roles.
64
     */
65
    public function getRoles() {
66
        return $this->roles;
67
    }
68
69
    /**
70
     * Get the user.
71
     *
72
     * @return UserInterface Returns the user.
73
     */
74
    public function getUser() {
75
        return $this->user;
76
    }
77
78
    /**
79
     * Set the roles;
80
     *
81
     * @param array $roles The roles.
82
     * @return BadUserRoleException Returns this bad user role exception.
83
     */
84
    protected function setRoles(array $roles) {
85
        $this->roles = $roles;
86
        return $this;
87
    }
88
89
    /**
90
     * Set the user.
91
     *
92
     * @param UserInterface $user The user.
93
     * @return BadUserRoleException Returns this bad user role exception.
94
     */
95
    protected function setUser(UserInterface $user) {
96
        $this->user = $user;
97
        return $this;
98
    }
99
100
}
101