Completed
Push — master ( a8f028...4e52eb )
by Oscar
05:21
created

FormatFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 4 1
A getAdmin() 0 4 1
A get() 0 10 2
1
<?php
2
3
namespace Folk\Formats;
4
5
use Folk\Admin;
6
use InvalidArgumentException;
7
8
/**
9
 * Class to create instances of formats.
10
 */
11
class FormatFactory
12
{
13
    private $admin;
14
15
    public function __construct(Admin $admin)
16
    {
17
        $this->admin = $admin;
18
    }
19
20
    /**
21
     * Returns a format instance
22
     *
23
     * @param string $name
24
     * @param array  $arguments
25
     */
26
    public function __call($name, $arguments)
27
    {
28
        return $this->get($name, $arguments);
29
    }
30
31
    /**
32
     * Returns the admin instance
33
     *
34
     * @return Admin
35
     */
36
    public function getAdmin(): Admin
37
    {
38
        return $this->admin;
39
    }
40
41
    /**
42
     * Returns a format instance.
43
     *
44
     * @param string $name
45
     * @param array  $arguments
46
     *
47
     * @return FormatInterface
48
     */
49
    private function get($name, array $arguments): FormatInterface
50
    {
51
        $class = 'Folk\\Formats\\'.ucfirst($name);
52
53
        if (!class_exists($class)) {
54
            throw new InvalidArgumentException("The format {$name} does not exists");
55
        }
56
57
        return new $class($this, ...$arguments);
58
    }
59
}
60