Factory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 57
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A make() 0 16 3
A has() 0 4 2
A validate() 0 6 2
1
<?php
2
3
namespace Njasm\Soundcloud\Factory;
4
5
6
7
class Factory implements FactoryInterface
8
{
9
    private $map = array(
10
        'AuthInterface'         => 'Njasm\\Soundcloud\\Auth\\Auth',
11
        'RequestInterface'      => 'Njasm\\Soundcloud\\Request\\Request',
12
        'ResponseInterface'     => 'Njasm\\Soundcloud\\Request\\Response',
13
        'ResourceInterface'     => 'Njasm\\Soundcloud\\Resource\\Resource',
14
        'UrlBuilderInterface'   => 'Njasm\\Soundcloud\\UrlBuilder\\UrlBuilder',
15
        'FactoryInterface'      => 'Njasm\\Soundcloud\\Factory\\Factory'
16
    );
17
    
18 1
    public function register($interface, $class)
19
    {
20 1
        $this->validate($interface);
21 1
        $this->map[$interface] = $class;
22
        
23 1
        return $this;
24
    }
25
26
    /**
27
     * @param string $interface
28
     * @param array $params
29
     * @return object
30
     */
31 9
    public function make($interface, array $params = array())
32
    {
33 9
        $this->validate($interface);
34
        
35 8
        if ($this->has($interface) === false) {
36 1
            throw new \InvalidArgumentException("You should register $interface in the Factory first.");
37
        }
38
39 7
        $reflected = new \ReflectionClass($this->map[$interface]);
40
41 7
        if (empty($params)) {
42 1
            return $reflected->newInstanceArgs();
43
        }
44
45 6
        return $reflected->newInstanceArgs($params);
46
    }
47
    
48 10
    public function has($interface)
49
    {
50 10
        return isset($this->map[$interface]) === true ? true : false;
51
    }
52
    
53
    /**
54
     * @param string $interface
55
     * @throws \InvalidArgumentException
56
     */
57 10
    private function validate($interface)
58
    {
59 10
        if (empty($interface)) {
60 1
            throw new \InvalidArgumentException("Invalid interface requested");
61
        }
62 9
    }
63
}
64