Test Failed
Push — master ( d60cf3...7665f3 )
by Julien
04:49
created

DeploymentTrait::dropAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
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(?string $models = null): array
85
    {
86
        $response = [
87
            'saved' => 0,
88
            'error' => [],
89
            'message' => [],
90
        ];
91
        
92
        $models = (!empty($models))? explode(',', $models) : null;
93
        
94
        foreach ($this->insert as $modelName => $insert) {
95
            if (is_array($models) && !in_array($modelName, $models, true)) {
96
                continue;
97
            }
98
            
99
            foreach ($insert as $key => $row) {
100
                $entity = new $modelName();
101
                assert($entity instanceof ModelInterface);
102
                
103
                $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

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