Passed
Push — bouncer-from-strings ( 831a9e )
by Daniel
02:52
created

BouncerFactory::createDeny()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * This file is part of the theroadbunch/bouncer package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
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 RoadBunch\Bouncer;
13
14
15
/**
16
 * Class BouncerFactory
17
 *
18
 * @author Dan McAdams <[email protected]>
19
 */
20
class BouncerFactory
21
{
22
    public static function create(Rule $rule, array|string $subjects = []): BouncerInterface
23
    {
24
        if (is_string($subjects)) {
0 ignored issues
show
introduced by
The condition is_string($subjects) is always false.
Loading history...
25
            $trimmed = [];
26
            foreach (explode(';', $subjects) as $subject) {
27
                if (!empty($subject)) {
28
                    $trimmed[] = trim($subject);
29
                }
30
            }
31
            $subjects = $trimmed;
32
        }
33
        return match ($rule) {
34
            Rule::ALLOW => new AllowList($subjects),
35
            Rule::DENY => new DenyList($subjects),
36
        };
37
    }
38
39
    public static function createAllow(array|string $subjects = []): BouncerInterface
40
    {
41
        return self::create(Rule::ALLOW, $subjects);
42
    }
43
44
    public static function createDeny(array|string $subjects = []): BouncerInterface
45
    {
46
        return self::create(Rule::DENY, $subjects);
47
    }
48
}
49