Database::getOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * This file is part of the Scrawler package.
4
 *
5
 * (c) Pranjal Pandey <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Scrawler\Arca\Facade;
14
15
use Scrawler\Arca\Collection;
16
use Scrawler\Arca\Connection;
0 ignored issues
show
Bug introduced by
The type Scrawler\Arca\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Scrawler\Arca\Database as DB;
18
use Scrawler\Arca\Factory\DatabaseFactory;
19
use Scrawler\Arca\Model;
20
use Scrawler\Arca\QueryBuilder;
21
22
class Database
23
{
24
    /**
25
     * Store the instance of current connection.
26
     */
27
    private static DB $database;
28
29
    /**
30
     * Create a new Database instance.
31
     *
32
     * @param array<mixed> $connectionParams
33
     */
34
    public static function connect(array $connectionParams): DB
35
    {
36
        $factory = new DatabaseFactory();
37
        self::$database = $factory->build($connectionParams);
38
39
        return self::$database;
40
    }
41
42
    /**
43
     * Get the instance of current connection.
44
     */
45
    private static function getDB(): DB
46
    {
47
        return self::$database;
48
    }
49
50
    /**
51
     * Create a new model.
52
     */
53
    public static function create(string $name): Model
54
    {
55
        return self::getDB()->create($name);
56
    }
57
58
    /**
59
     * Save a model.
60
     *
61
     * @param string $table
62
     */
63
    public static function get($table): Collection
64
    {
65
        return self::getDB()->get($table);
66
    }
67
68
    /**
69
     * Save a model.
70
     */
71
    public static function getOne(string $table, mixed $id): ?Model
72
    {
73
        return self::getDB()->getOne($table, $id);
74
    }
75
76
    /**
77
     * Execure a raw sql query.
78
     *
79
     * @return int|numeric-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|numeric-string at position 2 could not be parsed: Unknown type name 'numeric-string' at position 2 in int|numeric-string.
Loading history...
80
     */
81
    public static function exec(string $sql): int|string
82
    {
83
        return self::getDB()->exec($sql);
84
    }
85
86
    /**
87
     * Delete a model.
88
     */
89
    public static function delete(Model $model): mixed
90
    {
91
        return self::getDB()->delete($model);
92
    }
93
94
    /**
95
     * QUery builder to find a model.
96
     */
97
    public static function find(string $table): QueryBuilder
98
    {
99
        return self::getDB()->find($table);
100
    }
101
}
102