Test Failed
Push — master ( 4d9b91...c71310 )
by Julien
06:31
created

Dynamic::setDynamicMetaData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Phalcon\Cache\Adapter\Apcu;
15
use Phalcon\Mvc\Model\MetaData;
16
use Zemit\Mvc\Model;
17
use Zemit\Support\Utils;
18
19
/**
20
 * @todo, fix phalcon models meta data instead of just resetting it
21
 */
22
class Dynamic extends Model
23
{
24
    protected $_metaData = [];
25
    protected $_columnMap = [];
26
    
27
    public function initialize(): void
28
    {
29
        $this->setConnectionService('dbd');
30
        $this->setReadConnectionService('dbd');
31
        $this->setWriteConnectionService('dbd');
32
        
33
        // force delete cache for dynamic models
34
        // @todo find a better way to handle dynamic models using
35
        // meta data strategy or by overriding models meta data caching adapter etc.
36
        // ->reset didn't work for unknown reason
37
        $modelsMetaData = $this->getModelsMetaData();
38
        assert($modelsMetaData instanceof MetaData);
39
        $adapter = $modelsMetaData->getAdapter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $adapter is correct as $modelsMetaData->getAdapter() targeting Phalcon\Mvc\Model\MetaData::getAdapter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
        if ($adapter instanceof Apcu) {
41
            $lowerClassName = strtolower(Utils::getName($this));
42
            $adapter->delete('meta-' . $lowerClassName);
43
            $adapter->delete('map-' . $lowerClassName);
44
        }
45
        
46
        parent::initialize();
47
    }
48
    
49
    /**
50
     * Set the source table dynamically.
51
     */
52
    public function setDynamicSource(string $table): void
53
    {
54
        $this->setSource($table);
55
    }
56
    
57
    /**
58
     * Sets dynamic metadata for the object.
59
     */
60
    public function setDynamicMetaData(array $metaData): void
61
    {
62
        $this->_metaData = $metaData;
63
    }
64
    
65
    /**
66
     * Set the column mapping dynamically.
67
     */
68
    public function setDynamicColumnMap(array $map): void
69
    {
70
        $this->_columnMap = $map;
71
    }
72
    
73
    /**
74
     * Retrieves the metadata associated with the object.
75
     *
76
     * @return array The metadata as an associative array. Returns an empty array if no metadata is set.
77
     */
78
//    public function metaData(): array
79
//    {
80
//        return $this->_metaData ??= $this->getModelsMetaData()->readMetaData($this);
81
//    }
82
    
83
    /**
84
     * Dynamically set the column mapping.
85
     * Phalcon uses this to map database columns to model attributes.
86
     */
87
    public function columnMap(): array
88
    {
89
        if (empty($this->_columnMap)) {
90
            // Dynamically load column names from the database (or handle them dynamically)
91
            $metadata = $this->getModelsMetaData();
92
            $dataTypes = $metadata->getDataTypes($this);
93
            $columnMap = array_keys($dataTypes);
94
            
95
            // Store and return the dynamically generated column map
96
            $this->_columnMap = array_combine($columnMap, $columnMap);
97
        }
98
        
99
        return $this->_columnMap;
100
    }
101
    
102
    /**
103
     * Create a dynamic instance with a specific source and column map.
104
     */
105
    public static function createInstance(string $source, array $columnMap = []): Dynamic
106
    {
107
        $instance = new self();
108
        $instance->setDynamicSource($source);
109
        if (!empty($columnMap)) {
110
            $instance->setDynamicColumnMap($columnMap);
111
        }
112
        return $instance;
113
    }
114
}
115