Sqlite::pluginName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fabrica\Tools\Plugin;
4
5
use PDO;
6
use Fabrica\Tools\Builder;
7
use Fabrica\Models\Infra\Ci\Build;
8
use Fabrica\Tools\Plugin;
9
10
/**
11
 * SQLite Plugin — Provides access to a SQLite database.
12
 *
13
 * @author Dmitry Khomutov <[email protected]>
14
 */
15
class Sqlite extends Plugin
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $queries = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * @return string
29
     */
30
    public static function pluginName()
31
    {
32
        return 'sqlite';
33
    }
34
    
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function __construct(Builder $builder, Build $build, array $options = [])
39
    {
40
        parent::__construct($builder, $build, $options);
41
42
        $buildSettings = $this->builder->getConfig('build_settings');
43
44
        if (isset($buildSettings['sqlite'])) {
45
            $sql        = $buildSettings['sqlite'];
46
            $this->path = $sql['path'];
47
        }
48
    }
49
50
    /**
51
     * Connects to SQLite and runs a specified set of queries.
52
     *
53
     * @return bool
54
     */
55 View Code Duplication
    public function execute()
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...
56
    {
57
        try {
58
            $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
59
            $pdo  = new PDO('sqlite:' . $this->path, $opts);
60
61
            foreach ($this->queries as $query) {
62
                $pdo->query($this->builder->interpolate($query));
63
            }
64
        } catch (\Exception $ex) {
65
            $this->builder->logFailure($ex->getMessage());
66
            return false;
67
        }
68
        return true;
69
    }
70
}
71