ModelFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A factory() 0 14 3
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