Completed
Push — master ( fc0ef9...fd9cb0 )
by Andrew
04:08
created

EntityFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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