Passed
Pull Request — v1 (#21)
by
unknown
06:11
created

ConnectVariable::open()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Connect plugin for Craft CMS 3.x
4
 *
5
 * Allows you to connect to external databases and perform db queries
6
 *
7
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\connect\variables;
12
13
use nystudio107\connect\Connect;
14
use nystudio107\connect\db\Query;
15
use nystudio107\connect\db\Schema;
16
use nystudio107\connect\models\Settings;
17
18
use Craft;
19
20
use yii\db\Connection;
21
use yii\db\Exception;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
25
 * @package   Connect
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
27
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
28
class ConnectVariable
29
{
30
    // Public Methods
31
    // =========================================================================
32
33
    /**
34
     * Open a new database connection
35
     *
36
     * @param string $configName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     *
38
     * @return null|Connection
39
     */
40
    public function open(string $configName)
41
    {
42
        $connection = null;
43
        $config = $this->getDbConfig($configName);
44
        if (!empty($config)) {
45
            // Create a connection to the db
46
            $connection = new Connection($config);
47
            try {
48
                $connection->open();
49
            } catch (Exception $e) {
50
                Craft::error($e->getMessage(), __METHOD__);
51
            }
52
        }
53
54
        return $connection;
55
    }
56
57
    /**
58
     * Returns a new generic query.
59
     *
60
     * @param null|Connection $connection
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     *
62
     * @return Query
63
     */
64
    public function query($connection = null): Query
65
    {
66
        return new Query([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
67
            'db' => $connection,
68
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
69
    }
70
71
    /**
72
     * Returns a new generic schema.
73
     *
74
     * @param null|Connection $connection
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     *
76
     * @return Schema
77
     */
78
    public function schema($connection = null): Schema
79
    {
80
        return new Schema([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
81
            'db' => $connection,
82
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
83
    }
84
85
    // Protected Methods
86
    // =========================================================================
87
88
    /**
89
     * Return a database config with the key $configName from the Settings
90
     *
91
     * @param string $configName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
92
     *
93
     * @return array
94
     */
95
    protected function getDbConfig(string $configName): array
96
    {
97
        $config = [];
98
        /* @var Settings $settings */
99
        $settings = Connect::$plugin->getSettings();
100
        if ($settings !== null && !empty($settings->connections[$configName])) {
101
            $dbConnection = $settings->connections[$configName];
102
            $config = [
103
                'dsn' => "{$dbConnection['driver']}:"
104
                    ."host={$dbConnection['server']};"
105
                    ."dbname={$dbConnection['database']};"
106
                    ."port={$dbConnection['port']};",
107
                'username' => $dbConnection['user'],
108
                'password' => $dbConnection['password'],
109
                'tablePrefix' => $dbConnection['tablePrefix'],
110
            ];
111
        }
112
113
        return $config;
114
    }
115
}
116