ModelFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 50
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validateClass() 0 7 2
A prepareClassName() 0 3 1
A create() 0 6 1
1
<?php
2
3
namespace ddlzz\AmoAPI\Model;
4
5
use ddlzz\AmoAPI\Exception\EntityFactoryException;
6
use ddlzz\AmoAPI\SettingsStorage;
7
8
/**
9
 * Class ModelFactory.
10
 *
11
 * @author ddlzz
12
 */
13
class ModelFactory
14
{
15
    /** @var SettingsStorage */
16
    private $settings;
17
18
    /**
19
     * @param SettingsStorage $settings
20
     */
21 3
    public function __construct(SettingsStorage $settings)
22
    {
23 3
        $this->settings = $settings;
24 3
    }
25
26
    /**
27
     * @param string $type
28
     *
29
     * @return ModelInterface
30
     */
31 2
    public function create($type)
32
    {
33 2
        $entity = $this->prepareClassName($type);
34 2
        $this->validateClass($entity);
35
36 1
        return new $entity();
37
    }
38
39
    /**
40
     * @param string $name
41
     *
42
     * @return string
43
     */
44 2
    private function prepareClassName($name)
45
    {
46 2
        return SettingsStorage::NAMESPACE_PREFIX.'\Model\Amo\\'.rtrim(ucfirst($name), 's');
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @throws EntityFactoryException
53
     *
54
     * @return bool
55
     */
56 2
    private function validateClass($name)
57
    {
58 2
        if (!class_exists($name)) {
59 1
            throw new EntityFactoryException("Class \"$name\" does not exists");
60
        }
61
62 1
        return true;
63
    }
64
}
65