Passed
Push — develop ( 80cf75...2ccbbe )
by Nikolay
05:03
created

ModulesModelsBase::getConnectionServiceName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (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 General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Modules\Models;
21
22
use MikoPBX\Common\Models\ModelsBase;
23
use MikoPBX\Common\Providers\ConfigProvider;
24
use Phalcon\Text;
25
use Phalcon\Url;
26
use ReflectionClass as ReflectionClassAlias;
27
28
/**
29
 * Base class for module models.
30
 *
31
 * @package MikoPBX\Modules\Models
32
 */
33
class ModulesModelsBase extends ModelsBase
34
{
35
    protected bool $initialized = false;
36
    protected string $moduleUniqueId;
37
38
    /**
39
     * Class initialization and create DB connection by Class and DB name,
40
     * it is used in src/Common/Providers/ModulesDBConnectionsProvider.php.
41
     */
42
    public function initialize(): void
43
    {
44
        // Get child class parameters and define module UniqueID
45
        $reflector = new ReflectionClassAlias(static::class);
46
        $partsOfNameSpace = explode('\\', $reflector->getNamespaceName());
47
        if (count($partsOfNameSpace) === 3 && $partsOfNameSpace[0] === 'Modules') {
48
            $this->moduleUniqueId = $partsOfNameSpace[1];
49
            $this->setConnectionService(self::getConnectionServiceName($this->moduleUniqueId));
50
        }
51
        parent::initialize();
52
        $this->initialized = true;
53
    }
54
55
    /**
56
     * Returns the module name in a human-readable form for error and notification messages.
57
     *
58
     * @param bool $needLink Whether to include a link.
59
     *
60
     * @return string The module name.
61
     */
62
    public function getRepresent(bool $needLink = false): string
63
    {
64
        if (!$this->initialized) {
65
            $this->initialize();
66
        }
67
68
        if ($this->readAttribute('id') === null) {
69
            return $this->t('mo_NewElement');
70
        }
71
72
        if (isset($this->moduleUniqueId)) {
73
            $name = '<i class="puzzle piece icon"></i> '
74
                . $this->t('mo_' . $this->moduleUniqueId);
75
        } else {
76
            $name = 'Unknown';
77
        }
78
79
        if ($needLink) {
80
            if (empty($name)) {
81
                $name = $this->t('repLink');
82
            }
83
            $link = $this->getWebInterfaceLink();
84
85
            $result = $this->t(
86
                'rep' . $this->moduleUniqueId,
87
                [
88
                    'represent' => "<a href='{$link}'>{$name}</a>",
89
                ]
90
            );
91
        } else {
92
            $result = $name;
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Returns the link to the database record in the web interface.
100
     *
101
     * @return string The web interface link.
102
     */
103
    public function getWebInterfaceLink(): string
104
    {
105
        if (!$this->initialized) {
106
            $this->initialize();
107
        }
108
        if (isset($this->moduleUniqueId)) {
109
            $url = new Url();
110
            $baseUri = $this->di->getShared(ConfigProvider::SERVICE_NAME)->path('adminApplication.baseUri');
111
            $unCamelizedModuleId = Text::uncamelize($this->moduleUniqueId, '-');
112
            $link = $url->get("$unCamelizedModuleId/$unCamelizedModuleId/index", null, null, $baseUri);
113
        } else {
114
            $link = '#';
115
        }
116
117
        return $link;
118
    }
119
120
    /**
121
     * Returns the connection service name.
122
     *
123
     * @param string $moduleUniqueId Module UniqueID
124
     * @return string   The connection service name
125
     */
126
    public static function getConnectionServiceName(string $moduleUniqueId): string
127
    {
128
        return "{$moduleUniqueId}_module_db";
129
    }
130
}