PDO   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 38
c 1
b 0
f 0
dl 0
loc 82
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getExecuteStatementProcess() 0 3 1
A setDbConfigurationManager() 0 3 1
A getExecuteFileProcess() 0 3 1
A setInstanceName() 0 3 1
A resultSetToArray() 0 4 1
B getProcess() 0 44 7
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Db3v4l\Core\SqlExecutor\Forked;
4
5
use Db3v4l\API\Interfaces\SqlExecutor\Forked\CommandExecutor;
6
use Db3v4l\Service\DatabaseConfigurationManager;
7
use Db3v4l\Util\Process;
8
9
/**
10
 * Executes sql queries via a separate symfony console command.
11
 * Relies on the 'sql:execute' dbconsole command
12
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
13
class PDO extends ForkedExecutor implements CommandExecutor
14
{
15
    const EXECUTION_STRATEGY = 'PDO';
16
17
    protected $instanceName;
18
    /** @var DatabaseConfigurationManager $dbConfigurationManager */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
19
    protected $dbConfigurationManager;
20
21
    public function setInstanceName($instanceName)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setInstanceName()
Loading history...
22
    {
23
        $this->instanceName = $instanceName;
24
    }
25
26
    public function setDbConfigurationManager(DatabaseConfigurationManager $dbConfigurationManager)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setDbConfigurationManager()
Loading history...
27
    {
28
        $this->dbConfigurationManager = $dbConfigurationManager;
29
    }
30
31
    public function getExecuteStatementProcess($sql)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getExecuteStatementProcess()
Loading history...
32
    {
33
        return $this->getProcess($sql, self::EXECUTE_COMMAND);
34
    }
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @param string $filename
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
38
     * @return Process
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
39
     */
40
    public function getExecuteFileProcess($filename)
41
    {
42
        return $this->getProcess($filename, self::EXECUTE_FILE);
43
    }
44
45
    protected function getProcess($sqlOrFilename, $action = self::EXECUTE_COMMAND)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getProcess()
Loading history...
46
    {
47
        // pass on _all_ env vars, including PATH. Not doing so is deprecated...
48
        $env = null;
49
50
        $command = 'php';
51
        $options = [
52
            'bin/dbconsole',
53
            'sql:execute',
54
            '--only-instances=' . $this->instanceName,
55
            '--execute-in-process',
56
            '--execution-strategy=' . static::EXECUTION_STRATEGY,
57
            '--output-type=json',
58
        ];
59
60
        switch ($action) {
61
            case self::EXECUTE_COMMAND:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
62
                $options[] = '--sql=' . $sqlOrFilename;
63
                break;
64
            case self::EXECUTE_FILE:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
65
                $options[] = '--file=' . $sqlOrFilename;
66
                break;
67
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
68
                throw new \OutOfBoundsException('Unsupported action: $action');
69
        }
70
71
        $instanceConfiguration = $this->dbConfigurationManager->getInstanceConfiguration($this->instanceName);
72
73
        if ($instanceConfiguration != $this->databaseConfiguration) {
74
            if (isset($this->databaseConfiguration['dbname'])) {
75
                $options[] = '--database=' . $this->databaseConfiguration['dbname'];
76
            }
77
            if (isset($this->databaseConfiguration['user'])) {
78
                $options[] = '--user=' . $this->databaseConfiguration['user'];
79
            }
80
            if (isset($this->databaseConfiguration['password'])) {
81
                $options[] = '--password=' . $this->databaseConfiguration['password'];
82
            }
83
        }
84
85
        $commandLine = $this->buildCommandLine($command, $options);
86
87
        /// @todo create Process from array
88
        return new Process($commandLine, null, $env);
89
    }
90
91
    public function resultSetToArray($data)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resultSetToArray()
Loading history...
92
    {
93
        $data = json_encode($data);
94
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type string which is incompatible with the return type mandated by Db3v4l\API\Interfaces\Sq...tor::resultSetToArray() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
95
    }
96
}
97