Completed
Branch BUG/double-ampersand-in-regist... (7dce02)
by
unknown
131:59 queued 62:17
created

CalculatedModelFieldsFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromModel() 0 4 1
A createFromClassname() 0 11 2
1
<?php
2
3
namespace EventEspresso\core\libraries\rest_api\calculations;
4
5
use EventEspresso\core\domain\services\factories\FactoryInterface;
6
use EventEspresso\core\exceptions\UnexpectedEntityException;
7
use EventEspresso\core\services\loaders\LoaderInterface;
8
9
/**
10
 * Class CalculatedModelFieldsFactory
11
 *
12
 * Loads classes for calculating fields for the REST API
13
 *
14
 * @package     Event Espresso
15
 * @author         Mike Nelson
16
 * @since         $VID:$
17
 *
18
 */
19
class CalculatedModelFieldsFactory
20
{
21
    private $loader;
22
23
    /**
24
     * CalculatedModelFieldsFactory constructor.
25
     * @param LoaderInterface $loader
26
     */
27
    public function __construct(LoaderInterface $loader)
28
    {
29
        $this->loader = $loader;
30
    }
31
32
    /**
33
     * Creates the calculator class that corresponds to that particular model
34
     * @since $VID:$
35
     * @param string $model_name
36
     * @return Base
37
     * @throws UnexpectedEntityException
38
     */
39
    public function createFromModel($model_name)
40
    {
41
        return $this->createFromClassname('EventEspresso\core\libraries\rest_api\calculations\\' . $model_name);
42
    }
43
44
    /**
45
     * Creates the calculator class that corresponds to that classname and verifies it's of the correct type
46
     * @param string $calculator_classname
47
     * @return Base
48
     * @throws UnexpectedEntityException
49
     */
50
    public function createFromClassname($calculator_classname)
51
    {
52
        $calculator = $this->loader->getShared($calculator_classname);
53
        if (!$calculator instanceof Base) {
54
            throw new UnexpectedEntityException(
55
                $calculator_classname,
56
                'EventEspresso\core\libraries\rest_api\calculations\Base'
57
            );
58
        }
59
        return $calculator;
60
    }
61
}
62
// End of file CalculationsFactory.php
63
// Location: EventEspresso\core\libraries\rest_api\calculations/CalculationsFactory.php
64