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

EntityFactory::prepareClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
}