Test Failed
Push — master ( f3ed28...cc3204 )
by Carlos
09:07
created

InitClass   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 127
rs 10
wmc 24

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateTableData() 0 6 2
A getNamespace() 0 3 1
D loadExtension() 0 76 19
A loadBusinessDocumentExtension() 0 8 2
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2018-2024 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScripts\Core\Template;
21
22
use FacturaScripts\Core\Base\DataBase;
23
use FacturaScripts\Core\Tools;
24
use FacturaScripts\Dinamic\Lib\Import\CSVImport;
25
26
/**
27
 * Description of InitClass
28
 *
29
 * @author Carlos García Gómez <[email protected]>
30
 */
31
abstract class InitClass
32
{
33
    /**
34
     * Code to load every time FacturaScripts starts.
35
     */
36
    abstract public function init(): void;
37
38
    /**
39
     * Code that is executed when uninstalling a plugin.
40
     */
41
    abstract public function uninstall(): void;
42
43
    /**
44
     * Code to load every time the plugin is enabled or updated.
45
     */
46
    abstract public function update(): void;
47
48
    protected function getNamespace(): string
49
    {
50
        return substr(static::class, 0, -5);
51
    }
52
53
    /**
54
     * @param mixed $extension
55
     *
56
     * @return bool
57
     */
58
    protected function loadExtension($extension): bool
59
    {
60
        $namespace = get_class($extension);
61
        $findNamespace = $this->getNamespace() . '\\Extension\\';
62
        if (strpos($namespace, $findNamespace) !== 0) {
63
            Tools::log()->error('Target object not found for: ' . $namespace);
64
            return false;
65
        }
66
67
        $className = substr($namespace, strlen($findNamespace));
68
        switch ($className) {
69
            case 'Model\\Base\\BusinessDocument':
70
                return $this->loadBusinessDocumentExtension($extension, [
71
                    'AlbaranCliente', 'AlbaranProveedor', 'FacturaCliente', 'FacturaProveedor',
72
                    'PedidoCliente', 'PedidoProveedor', 'PresupuestoCliente', 'PresupuestoProveedor'
73
                ]);
74
75
            case 'Model\\Base\\BusinessDocumentLine':
76
                return $this->loadBusinessDocumentExtension($extension, [
77
                    'LineaAlbaranCliente', 'LineaAlbaranProveedor', 'LineaFacturaCliente',
78
                    'LineaFacturaProveedor', 'LineaPedidoCliente', 'LineaPedidoProveedor',
79
                    'LineaPresupuestoCliente', 'LineaPresupuestoProveedor'
80
                ]);
81
82
            case 'Model\\Base\\PurchaseDocument':
83
                return $this->loadBusinessDocumentExtension($extension, [
84
                    'AlbaranProveedor', 'FacturaProveedor', 'PedidoProveedor', 'PresupuestoProveedor'
85
                ]);
86
87
            case 'Model\\Base\\PurchaseDocumentLine':
88
                return $this->loadBusinessDocumentExtension($extension, [
89
                    'LineaAlbaranProveedor', 'LineaFacturaProveedor', 'LineaPedidoProveedor', 'LineaPresupuestoProveedor'
90
                ]);
91
92
            case 'Model\\Base\\SalesDocument':
93
                return $this->loadBusinessDocumentExtension($extension, [
94
                    'AlbaranCliente', 'FacturaCliente', 'PedidoCliente', 'PresupuestoCliente'
95
                ]);
96
97
            case 'Model\\Base\\SalesDocumentLine':
98
                return $this->loadBusinessDocumentExtension($extension, [
99
                    'LineaAlbaranCliente', 'LineaFacturaCliente', 'LineaPedidoCliente', 'LineaPresupuestoCliente'
100
                ]);
101
102
            case 'Controller\\EditController':
103
                // recorremos todos los controlados que empiezan por Edit
104
                $controllers = Tools::folderScan(FS_FOLDER . '/Dinamic/Controller/');
105
                foreach ($controllers as $file) {
106
                    $controller = '\\FacturaScripts\\Dinamic\\Controller\\' . substr($file, 0, -4);
107
108
                    if (str_starts_with($file, 'Edit') && str_ends_with($file, '.php') && class_exists($controller)) {
109
                        $controller::addExtension($extension);
110
                    }
111
                }
112
                return true;
113
114
            case 'Controller\\ListController':
115
                // recorremos todos los controlados que empiezan por List
116
                $controllers = Tools::folderScan(FS_FOLDER . '/Dinamic/Controller/');
117
                foreach ($controllers as $file) {
118
                    $controller = '\\FacturaScripts\\Dinamic\\Controller\\' . substr($file, 0, -4);
119
120
                    if (str_starts_with($file, 'List') && str_ends_with($file, '.php') && class_exists($controller)) {
121
                        $controller::addExtension($extension);
122
                    }
123
                }
124
                return true;
125
        }
126
127
        $targetClass = '\\FacturaScripts\\Dinamic\\' . $className;
128
        if (class_exists($targetClass)) {
129
            $targetClass::addExtension($extension);
130
            return true;
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * @param mixed $extension
138
     * @param array $models
139
     *
140
     * @return bool
141
     */
142
    private function loadBusinessDocumentExtension($extension, $models): bool
143
    {
144
        foreach ($models as $model) {
145
            $targetClass = '\\FacturaScripts\\Dinamic\\Model\\' . $model;
146
            $targetClass::addExtension($extension);
147
        }
148
149
        return true;
150
    }
151
152
    protected function updateTableData(string $tableName): void
153
    {
154
        $sql = CSVImport::updateTableSQL($tableName);
155
        if ($sql) {
156
            $dataBase = new DataBase();
157
            $dataBase->exec($sql);
158
        }
159
    }
160
}
161