ModelFactory::factory()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 8
nop 2
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Guillermoandrae\Highrise\Models;
4
5
use Guillermoandrae\Models\InvalidModelException;
6
7
final class ModelFactory
8
{
9
    /**
10
     * Returns the desired model using the provided data.
11
     *
12
     * @param string $name The name of the desired model.
13
     * @param string $xml The XML.
14
     * @return ModelInterface
15
     * @throws InvalidModelException  Thrown when an invalid model is
16
     *                                requested.
17
     */
18
    public static function factory(string $name, string $xml): ModelInterface
19
    {
20
        try {
21
            $name = $name == 'kases' ? 'case' : $name;
22
            $className = sprintf(
23
                '%s\%sModel',
24
                __NAMESPACE__,
25
                ucfirst(strtolower($name))
26
            );
27
            $reflectionClass = new \ReflectionClass($className);
28
            return $reflectionClass->newInstance($xml);
29
        } catch (\ReflectionException $ex) {
30
            throw new InvalidModelException(
31
                sprintf('The %s model does not exist.', $name)
32
            );
33
        }
34
    }
35
}
36