Bans::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_bans;
4
5
6
use kalanis\kw_bans\Bans\ABan;
7
use kalanis\kw_bans\Interfaces\IKBTranslations;
8
9
10
/**
11
 * Class Bans
12
 * @package kalanis\kw_bans
13
 * One of main libraries to process detection if that querying system is on internal lists
14
 */
15
class Bans
16
{
17
    /** @var Bans\ABan[] */
18
    protected array $sources = [];
19
20
    /**
21
     * @param IKBTranslations|null $lang
22
     * @param string|array<string>|array<int, string>|Sources\ASources $sources
23
     * @throws BanException
24
     */
25 3
    public function __construct(?IKBTranslations $lang = null, ...$sources)
26
    {
27 3
        $factory = new Bans\Factory($lang);
28 3
        foreach ($sources as $source) {
29 3
            $this->sources[] = $factory->whichType($source);
30
        }
31 3
    }
32
33
    /**
34
     * @param string|array<string>|array<int, string>|Sources\ASources ...$toCompare
35
     * @throws BanException
36
     * @return bool
37
     */
38 3
    public function has(...$toCompare): bool
39
    {
40 3
        $smallerSources = array_slice($this->sources, 0, min(count($this->sources), count($toCompare)));
41 3
        foreach ($smallerSources as $i => $source) {
42
            /** @var ABan $source */
43 3
            if (is_string($toCompare[$i]) && !empty($toCompare[$i])) {
44 3
                $source->setLookedFor($toCompare[$i]);
45 3
                if ($source->isBanned()) {
46 2
                    return true;
47
                }
48
            }
49
        }
50 1
        return false;
51
    }
52
}
53