Test Failed
Push — master ( 1fe578...f222e5 )
by Julien
05:09
created

DeploymentTrait::addModelsPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 6
rs 9.9666
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\Modules\Cli\Tasks\Traits;
13
14
use Phalcon\Mvc\ModelInterface;
15
use Zemit\Exception\CliException;
16
use Zemit\Utils;
17
18
trait DeploymentTrait
19
{
20
    
21
    
22
    /**
23
     * Default action
24
     * @throws CliException
25
     */
26
    public function mainAction(): ?array
27
    {
28
        $response = [];
29
        
30
        $response ['truncate'] = $this->truncateAction();
31
        $response ['drop'] = $this->dropAction();
32
        $response ['engine'] = $this->fixEngineAction();
33
        $response ['insert'] = $this->insertAction();
34
        
35
        return $response;
36
    }
37
    
38
    /**
39
     * Truncate tables
40
     */
41
    public function truncateAction(): array
42
    {
43
        $response = [];
44
        
45
        foreach ($this->truncate as $table) {
46
            $response [] = $this->db->execute('TRUNCATE TABLE ' . $this->db->escapeIdentifier($table));
47
        }
48
        
49
        return $response;
50
    }
51
    
52
    /**
53
     * Drops tables
54
     */
55
    public function dropAction(): array
56
    {
57
        $response = [];
58
        
59
        foreach ($this->drop as $table) {
60
            $response [] = $this->db->execute('DROP TABLE IF EXISTS ' . $this->db->escapeIdentifier($table));
61
        }
62
        
63
        return $response;
64
    }
65
    
66
    /**
67
     * Fix tables engine
68
     */
69
    public function fixEngineAction(): array
70
    {
71
        $response = [];
72
        
73
        foreach ($this->engine as $table => $engine) {
74
            $response [] = $this->db->execute('ALTER TABLE ' . $this->db->escapeIdentifier($table) . ' ENGINE = ' . $engine);
75
        }
76
        
77
        return $response;
78
    }
79
    
80
    /**
81
     * Insert records
82
     * @throws CliException
83
     */
84
    public function insertAction(): array
85
    {
86
        $response = [
87
            'saved' => 0,
88
            'error' => [],
89
            'message' => [],
90
        ];
91
        
92
        foreach ($this->insert as $modelName => $insert) {
93
            
94
            foreach ($insert as $key => $row) {
95
                $entity = new $modelName();
96
                assert($entity instanceof ModelInterface);
97
                
98
                $assign = isset($row[0]) ? array_combine($entity->columnMap(), $row) : $row;
0 ignored issues
show
Bug introduced by
The method columnMap() does not exist on Phalcon\Mvc\ModelInterface. It seems like you code against a sub-type of Phalcon\Mvc\ModelInterface such as Phalcon\Mvc\Model or Zemit\Models\Setting or Zemit\Models\Category or Zemit\Models\Audit or Zemit\Models\UserGroup or Zemit\Models\User or Zemit\Models\Field or Zemit\Models\Page or Zemit\Models\Log or Zemit\Models\File or Zemit\Models\Role or Zemit\Models\GroupRole or Zemit\Models\Template or Zemit\Models\AuditDetail or Zemit\Models\UserType or Zemit\Models\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\Email or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\SiteLang or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Site or Zemit\Models\Type or Zemit\Models\Channel or Zemit\Models\Meta. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
                $assign = isset($row[0]) ? array_combine($entity->/** @scrutinizer ignore-call */ columnMap(), $row) : $row;
Loading history...
99
                if (!$assign) {
100
                    throw new CliException('Can\'t assign row #' . $key . ' for model `' . $modelName . '`.');
101
                } else {
102
                    $entity->assign($assign);
103
                }
104
                
105
                // Automagically fill passwords
106
                if (property_exists($entity, 'password')) {
107
                    if (empty($row['password'])) {
108
                        $entity->assign(['password' => $row['username'], 'passwordConfirm' => $row['username']]);
109
                    }
110
                    elseif (empty($row['passwordConfirm'])) {
111
                        $entity->assign(['passwordConfirm' => $row['password']]);
112
                    }
113
                }
114
                
115
                if (!$entity->save()) {
116
                    $response['error'][$modelName][] = $entity->toArray();
117
                    
118
                    foreach ($entity->getMessages() as $message) {
119
                        $response['message'][$modelName][] = $message;
120
                    }
121
                }
122
                else {
123
                    $response['saved']++;
124
                }
125
            }
126
        }
127
        
128
        return $response;
129
    }
130
    
131
    public function addModelsPermissions(?array $tables = null): void
132
    {
133
        $permissions = [];
134
        $tables ??= $this->insert;
135
        foreach ($tables as $model => $entity) {
136
            $permissions[$model] = ['*'];
137
        }
138
        $this->config->merge([
139
            'permissions' => [
140
                'roles' => [
141
                    'cli' => [
142
                        'models' => $permissions
143
                    ],
144
                ]
145
            ]
146
        ]);
147
    }
148
}
149