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

CriteriaHandlersManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHandlerFor() 0 10 4
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