AuthHandlerCollection   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Madeline\Auth\Handler;
6
7
use Zored\Telegram\Madeline\Auth\Handler\Exception\AuthHandlerException;
8
use Zored\Telegram\Madeline\Config\Auth\AuthConfigInterface;
9
use Zored\Telegram\Util\Collection\ConditionMatch;
10
11
class AuthHandlerCollection implements AuthHandlerCollectionInterface
12
{
13
    /**
14
     * @var array|AuthHandlerInterface[]
15
     */
16
    private $handlers;
17
18
    /**
19
     * @param AuthHandlerInterface[] $handlers
20
     */
21
    public function __construct(array $handlers)
22
    {
23
        $this->handlers = $handlers;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function get(AuthConfigInterface $config): AuthHandlerInterface
30
    {
31
        $handler = (new ConditionMatch($this->handlers))->find(
32
            function (AuthHandlerInterface $currentHandler) use ($config) {
33
                return $currentHandler->suits($config);
34
            }
35
        );
36
37
        if (!$handler instanceof AuthHandlerInterface) {
38
            throw AuthHandlerException::becauseHandlerNotFound();
39
        }
40
41
        return $handler;
42
    }
43
}
44