ModelFactory::__construct()   A
last analyzed

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
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