Completed
Push — master ( bb528d...cbdf7c )
by Timur
01:35
created

CriteriaHandlersManager::__construct()   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
namespace Zurbaev\Achievements;
4
5
use Zurbaev\Achievements\Contracts\CriteriaHandler;
6
use Zurbaev\Achievements\Contracts\CriteriaHandlersManager as CriteriaHandlersManagerContract;
7
8
class CriteriaHandlersManager implements CriteriaHandlersManagerContract
9
{
10
    /** @var array */
11
    protected $handlers = [];
12
13
    /**
14
     * CriteriaHandlersManager constructor.
15
     *
16
     * @param array $handlers
17
     */
18
    public function __construct(array $handlers)
19
    {
20
        $this->handlers = $handlers;
21
    }
22
23
    /**
24
     * Get the handler for the given criteria type.
25
     *
26
     * @param string $type
27
     *
28
     * @return CriteriaHandler
29
     */
30
    public function getHandlerFor(string $type)
31
    {
32
        if (empty($this->handlers[$type])) {
33
            throw new \InvalidArgumentException('There is no handler for the "'.$type.'" criteria type.');
34
        } elseif (is_object($this->handlers[$type]) && $this->handlers[$type] instanceof CriteriaHandler) {
35
            return $this->handlers[$type];
36
        }
37
38
        return new $this->handlers[$type];
39
    }
40
}
41