Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

paragonie / easydb / 310

4 Apr 2018 - 4:01 coverage: 84.646%. First build
310

Pull #76

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Update EasyStatement.php
Pull Request #76: Housekeeping

9 of 10 new or added lines in 2 files covered. (90.0%)

419 of 495 relevant lines covered (84.65%)

108.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/src/Factory.php
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Paragon Initiative Enterprises.
5
 *
6
 * @author  Scott Arciszewski   <scott@paragonie.com>.
7
 * @author  EasyDB Contributors <https://github.com/paragonie/easydb/graphs/contributors>
8
 *
9
 * @link    <https://github.com/paragonie/easydb> Github Repository.
10
 * @license <https://github.com/paragonie/easydb/blob/master/LICENSE> MIT License.
11
 *
12
 * @package ParagonIE\EasyDB
13
 */
14

15
namespace ParagonIE\EasyDB;
16

17
use PDO;
18
use PDOException;
19

20
/**
21
 * Factory.
22
 */
23
abstract class Factory
24
{
25

26
    /**
27
     * Create a new EasyDB object based on PDO constructors.
28
     *
29
     * @param string      $dsn      The dns connection string.
30
     * @param string|null $username The database username.
31
     * @param string|null $password The database password.
32
     * @param array       $options  The database options.
33
     *
34
     * @throws Exception\ConstructorFailed If the PDO connection could
35
     *                                     not be created.
36
     *
37
     * @return EasyDB Return the EasyDB class.
38
     */
39
    public static function create(
40
        string $dsn,
41
        string $username = \null,
42
        string $password = \null,
43
        array  $options = []
44
    ): EasyDB {
45
        if (\is_null($username)) {
1,176×
46
            $username = '';
1,170×
47
        }
48
        if (\is_null($password)) {
1,176×
49
            $password = '';
1,170×
50
        }
51
        $dbEngine = '';
1,176×
52
        // Get the pdo driver.
53
        if (\strpos($dsn, ':') !== \false) {
1,176×
54
            // Explode the dns string into parts.
55
            $dbEngine = \explode(':', $dsn)[0];
1,170×
56
        }
57
        /** @var string $post_query */
58
        $post_query = '';
1,176×
59
        switch ($dbEngine) {
60
            case 'mysql':
1,176×
61
                // Set the charset.
NEW
62
                if (\strpos($dsn, ';charset=') === \false) {
!
63
                    $dsn .= ';charset=utf8mb4';
!
64
                }
65
                break;
!
66
            case 'pgsql':
1,176×
67
                // Set names unicode.
68
                $post_query = 'SET NAMES UNICODE';
!
69
                break;
!
70
        }
71
        try {
72
            // Try to create a new connection.
73
            $pdo = new PDO($dsn, $username, $password, $options);
1,176×
74
        } catch (PDOException $e) {
6×
75
            // Suppress errors to prevent sensitive information from being displayed.
76
            throw new Exception\ConstructorFailed(
6×
77
                'Could not create a PDO connection. Please check your username and password.'
6×
78
            );
79
        }
80
        // Execute post query if it is not empty.
81
        if (!empty($post_query)) {
1,170×
82
            $pdo->query($post_query);
!
83
        }
84
        // Return the EasyDB classs.
85
        return new EasyDB($pdo, $dbEngine, $options);
1,170×
86
    }
87
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc