Issues (151)

src/variables/ConnectVariable.php (22 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
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\models\Settings;
16
17
use Craft;
18
19
use yii\db\Connection;
20
use yii\db\Exception;
21
22
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
23
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
24
 * @package   Connect
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
26
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
27
class ConnectVariable
28
{
29
    // Public Methods
30
    // =========================================================================
31
32
    /**
33
     * Open a new database connection
34
     *
35
     * @param string $configName
0 ignored issues
show
Missing parameter comment
Loading history...
36
     *
37
     * @return null|Connection
38
     */
39
    public function open(string $configName)
40
    {
41
        $connection = null;
42
        $config = $this->getDbConfig($configName);
43
        if (!empty($config)) {
44
            // Create a connection to the db
45
            $connection = new Connection($config);
46
            try {
47
                $connection->open();
48
            } catch (Exception $e) {
49
                Craft::error($e->getMessage(), __METHOD__);
50
            }
51
        }
52
53
        return $connection;
54
    }
55
56
    /**
57
     * Returns a new generic query.
58
     *
59
     * @param null|Connection $connection
0 ignored issues
show
Missing parameter comment
Loading history...
60
     *
61
     * @return Query
62
     */
63
    public function query($connection = null): Query
64
    {
65
        return new Query([
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
66
            'db' => $connection,
67
        ]);
0 ignored issues
show
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...
68
    }
69
70
    // Protected Methods
71
    // =========================================================================
72
73
    /**
74
     * Return a database config with the key $configName from the Settings
75
     *
76
     * @param string $configName
0 ignored issues
show
Missing parameter comment
Loading history...
77
     *
78
     * @return array
79
     */
80
    protected function getDbConfig(string $configName): array
81
    {
82
        $config = [];
83
        /* @var Settings $settings */
84
        $settings = Connect::$plugin->getSettings();
85
        if ($settings !== null && !empty($settings->connections[$configName])) {
86
            $dbConnection = $settings->connections[$configName];
87
            $config = [
88
                'dsn' => "{$dbConnection['driver']}:"
89
                    ."host={$dbConnection['server']};"
90
                    ."dbname={$dbConnection['database']};"
91
                    ."port={$dbConnection['port']};",
92
                'username' => $dbConnection['user'],
93
                'password' => $dbConnection['password'],
94
                'tablePrefix' => $dbConnection['tablePrefix'],
95
            ];
96
        }
97
98
        return $config;
99
    }
100
}
101