Completed
Pull Request — master (#64)
by Marko
03:01
created

EntityChildrenFactory::entity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jul 5, 2016 - 3:05:38 AM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         EntityChildrenFactory.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 * @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Core\Helpers;
25
26
use \AframeVR\Core\Entity;
27
use \AframeVR\Core\Exceptions\BadPrimitiveCallException;
28
29
class EntityChildrenFactory
30
{
31
    /**
32
     * Child entities
33
     *
34
     * @var array
35
     */
36
    private $childrens = array();
37
38
    /**
39
     * Get entity
40
     * 
41
     * @param string $a_type
42
     * @param string $id
43
     * @return AframeVR\Interfaces\EntityInterface
44
     */
45 77
    public function getEntity(string $a_type, string $id)
46
    {
47 77
        $id = $id ?? 0;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $id. This often makes code more readable.
Loading history...
48 77
        return $this->childrens[$a_type][$id] ?? $this->addEntity($a_type, $id);
49
    }
50
    
51
    /**
52
     * Get all children for calling parent
53
     * @return array
54
     */
55 19
    public function getChildren()
56
    {
57 19
        return iterator_to_array($this->getChildrenRAW($this->childrens), false);
58
    }
59
    
60
    /**
61
     * Get entity
62
     * 
63
     * @param string $method
64
     * @param array $args
65
     * @return AframeVR\Interfaces\EntityInterface
66
     */
67 6
    public function __call(string $method, array $args)
68
    {
69 6
        $id = $args[0] ?? 0;
70 6
        return $this->getEntity($method, $id);
71
    }
72
    
73
    /**
74
     * Register new entity
75
     * 
76
     * Generally you should not call this method directly but if you want to extend
77
     * EntityChildrenFactory then you can still access it
78
     *
79
     * @param string $a_type            
80
     * @param string $id            
81
     * @throws BadShaderCallException
82
     * @return AframeVR\Interfaces\EntityInterface
83
     */
84 77
    protected function addEntity(string $a_type, string $id)
85
    {
86 77
        $primitive = sprintf('\AframeVR\Extras\Primitives\%s', ucfirst($a_type));
87
        
88 77
        if($a_type === 'entity') {
89 55
            return $this->childrens[$a_type][$id] = new Entity($id);
90 27
        } elseif (class_exists($primitive)) {
91 27
            return $this->childrens[$a_type][$id] = new $primitive($id);
92
        } else {
93 1
            throw new BadPrimitiveCallException($a_type);
94
        }
95
    }
96
    
97
    /**
98
     * Generate array of children
99
     * 
100
     * @param array $array
101
     */
102 19
    protected function getChildrenRAW(array $array) {
103 19
        foreach ($array as $value) {
104 11
            if (is_array($value)) {
105 11
                yield from $this->getChildrenRAW($value);
106
            } else {
107 11
                yield $value;
108
            }
109
        }
110
    }
111
}