• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LM-Commons / LmcRbac / 10637147434

27 Aug 2024 08:02PM UTC coverage: 99.219% (-0.8%) from 100.0%
10637147434

push

github

web-flow
Merge pull request #85 from visto9259/2.0.x

Updated docs. Moved to laminas rbac

28 of 28 new or added lines in 3 files covered. (100.0%)

2 existing lines in 1 file now uncovered.

254 of 256 relevant lines covered (99.22%)

4.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/Service/RoleService.php
1
<?php
2

3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19

20
declare(strict_types=1);
21

22
namespace Lmc\Rbac\Service;
23

24
use Laminas\Permissions\Rbac\RoleInterface;
25
use Lmc\Rbac\Identity\IdentityInterface;
26
use Lmc\Rbac\Role\RoleProviderInterface;
27
use Traversable;
28

29
use function array_merge;
30

31
/**
32
 * Role service
33
 */
34
class RoleService implements RoleServiceInterface
35
{
36
    protected RoleProviderInterface $roleProvider;
37

38
    protected string $guestRole = '';
39

40
    public function __construct(RoleProviderInterface $roleProvider, string $guestRole)
5✔
41
    {
42
        $this->roleProvider = $roleProvider;
5✔
43
        $this->guestRole    = $guestRole;
5✔
44
    }
45

46
    /**
47
     * Set the guest role
48
     *
49
     * @deprecated The Guest role should be defined using the constructor
50
     */
UNCOV
51
    public function setGuestRole(string $guestRole): void
×
52
    {
UNCOV
53
        $this->guestRole = $guestRole;
×
54
    }
55

56
    /**
57
     * Get the guest role
58
     */
59
    public function getGuestRole(): string
1✔
60
    {
61
        return $this->guestRole;
1✔
62
    }
63

64
    /**
65
     * Get the roles from the current identity, applying some more logic
66
     *
67
     * @return RoleInterface[]
68
     */
69
    public function getIdentityRoles(?IdentityInterface $identity = null): iterable
4✔
70
    {
71
        // If no identity is provided, get the guest role
72
        if (null === $identity) {
4✔
73
            return $this->convertRoles([$this->guestRole]);
2✔
74
        }
75

76
        return $this->convertRoles($identity->getRoles());
2✔
77
    }
78

79
    /**
80
     * Convert the roles (potentially strings) to concrete RoleInterface objects using role provider
81
     *
82
     * @param  string[]|RoleInterface[] $roles
83
     * @return RoleInterface[]
84
     */
85
    protected function convertRoles(array $roles): iterable
4✔
86
    {
87
        $collectedRoles = [];
4✔
88
        $toCollect      = [];
4✔
89
        foreach ($roles as $role) {
4✔
90
            // If it's already a RoleInterface, nothing to do as a RoleInterface contains everything already
91
            if ($role instanceof RoleInterface) {
4✔
92
                $collectedRoles[] = $role;
2✔
93
                continue;
2✔
94
            }
95

96
            // Otherwise, it's a string and hence we need to collect it
97
            $toCollect[] = $role;
3✔
98
        }
99

100
        // Nothing to collect, we don't even need to hit the (potentially) costly role provider
101
        if (empty($toCollect)) {
4✔
102
            return $collectedRoles;
1✔
103
        }
104

105
        return array_merge($collectedRoles, $this->roleProvider->getRoles($toCollect));
3✔
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc