Passed
Push — main ( 73aced...57fc6e )
by Pranjal
03:22
created

RecordManager::select()   A

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
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
namespace Scrawler\Arca\Manager;
12
13
use Doctrine\DBAL\Connection;
14
use Ramsey\Uuid\Uuid;
15
use Scrawler\Arca\Collection;
16
use Scrawler\Arca\Config;
17
use Scrawler\Arca\Model;
18
use Scrawler\Arca\QueryBuilder;
19
20
/**
21
 * Class responsible for manging single records.
22
 */
23
final class RecordManager
24
{
25
    /**
26
     * Create RecordManager.
27
     */
28
    public function __construct(
29
        private readonly Connection $connection,
30
        private readonly ModelManager $modelManager,
31
        private readonly Config $config,
32
    ) {
33
    }
34
35
    /**
36
     * Create a new record.
37
     */
38
    public function insert(Model $model): mixed
39
    {
40
        if ($this->config->isUsingUUID()) {
41
            $model->set('id', Uuid::uuid4()->toString());
42
        }
43
        $this->connection->insert($model->getName(), $model->getSelfProperties());
44
        if ($this->config->isUsingUUID()) {
45
            return $model->get('id');
46
        }
47
48
        return (int) $this->connection->lastInsertId();
49
    }
50
51
    /**
52
     * Update a record.
53
     */
54
    public function update(Model $model): mixed
55
    {
56
        $this->connection->update($model->getName(), $model->getSelfProperties(), ['id' => $model->getId()]);
57
58
        return $model->getId();
59
    }
60
61
    /**
62
     * Delete a record.
63
     */
64
    public function delete(Model $model): mixed
65
    {
66
        $this->connection->delete($model->getName(), ['id' => $model->getId()]);
67
68
        return $model->getId();
69
    }
70
71
    /**
72
     * Get single record by id.
73
     */
74
    public function getById(string $table, mixed $id): ?Model
75
    {
76
        $query = (new QueryBuilder($this->connection, $this->modelManager))
77
            ->select('*')
78
            ->from($table, 't')
79
            ->where('t.id = ?')
80
            ->setParameter(0, $id);
81
82
        return $query->first();
83
    }
84
85
    /**
86
     * Get all records.
87
     */
88
    public function getAll(string $tableName): Collection
89
    {
90
        return (new QueryBuilder($this->connection, $this->modelManager))
91
            ->select('*')
92
            ->from($tableName, 't')
93
            ->get();
94
    }
95
96
    /**
97
     * get query builder from db.
98
     */
99
    public function find(string $name): QueryBuilder
100
    {
101
        return (new QueryBuilder($this->connection, $this->modelManager))
102
            ->select('*')
103
            ->from($name, 't');
104
    }
105
106
    /**
107
     * get query builder from db.
108
     */
109
    public function select(string $expression): QueryBuilder
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    public function select(/** @scrutinizer ignore-unused */ string $expression): QueryBuilder

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        return (new QueryBuilder($this->connection, $this->modelManager));
112
    }
113
}
114