Completed
Push — master ( 02927c...6d0ead )
by Freek
05:08 queued 01:44
created

PgSQLDatabase::useCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Backup\BackupHandlers\Database\Databases;
4
5
use Spatie\Backup\Console;
6
7
class PgSQLDatabase implements DatabaseInterface
8
{
9
    protected $console;
10
    protected $database;
11
    protected $schema;
12
    protected $username;
13
    protected $password;
14
    protected $host;
15
    protected $port;
16
17
    /**
18
     * @param Console $console
19
     * @param $database
20
     * @param string $schema
21
     * @param string $username
22
     * @param $password
23
     * @param string string $host
24
     * @param int $port
25
     */
26 View Code Duplication
    public function __construct(Console $console, $database, $schema, $username, $password, $host, $port)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $this->console = $console;
29
        $this->database = $database;
30
        $this->schema = $schema;
31
        $this->username = $username;
32
        $this->password = $password;
33
        $this->host = $host;
34
        $this->port = $port;
35
    }
36
37
    /**
38
     * Create a database dump.
39
     *
40
     * @param $destinationFile
41
     *
42
     * @return bool
43
     */
44
    public function dump($destinationFile)
45
    {
46
        $command = sprintf('export PGHOST && %spg_dump ' . (!$this->useCopy() ? '--inserts' : '') . ' --schema=%s %s > %s',
47
            $this->getDumpCommandPath(),
48
            escapeshellarg($this->schema),
49
            escapeshellarg($this->database),
50
            escapeshellarg($destinationFile)
51
        );
52
53
        $env = [
54
            'PGHOST' => $this->host,
55
            'PGUSER' => $this->username,
56
            'PGPASSWORD' => $this->password,
57
            'PGPORT' => $this->port,
58
        ];
59
60
        return $this->console->run($command, config('laravel-backup.pgsql.timeoutInSeconds'), $env);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->console->run($com...eoutInSeconds'), $env); of type boolean|string adds the type string to the return on line 60 which is incompatible with the return type declared by the interface Spatie\Backup\BackupHand...DatabaseInterface::dump of type boolean.
Loading history...
61
    }
62
63
    /**
64
     * Return the file extension of a dump file (sql, ...).
65
     *
66
     * @return string
67
     */
68
    public function getFileExtension()
69
    {
70
        return 'sql';
71
    }
72
73
    /**
74
     * Get the path to the pgsql_dump.
75
     *
76
     * @return string
77
     */
78
    protected function getDumpCommandPath()
79
    {
80
        $path = config('laravel-backup.pgsql.dump_command_path');
81
82
        if ($path != '') {
83
            $path = str_finish($path, '/');
84
        }
85
86
        return $path;
87
88
        return $path;
0 ignored issues
show
Unused Code introduced by
return $path; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
89
    }
90
91
    /**
92
     * Determine if COPY should be used instead of INSERT.
93
     */
94
    protected function useCopy()
95
    {
96
        return config('laravel-backup.pgsql.use_copy');
97
    }
98
}
99