User   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 76
rs 10
ccs 36
cts 36
cp 1
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toSqlDrop() 0 7 1
A toSqlCreate() 0 8 1
A toSqlPrivileges() 0 11 2
A setGrant() 0 3 1
A getFactory() 0 5 1
A __construct() 0 5 1
A setGrants() 0 4 2
A setPlatform() 0 3 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[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
namespace FlexPHP\Database;
11
12
use FlexPHP\Database\Interfaces\UserFactoryInterface;
13
14
final class User implements UserInterface
15
{
16
    private string $name;
17
18
    private string $password;
19
20
    private string $host;
21
22
    /**
23
     * @var array<int, array>
24
     */
25
    private array $grants = [];
26
27
    private string $platform = 'MySQL';
28
29 104
    public function __construct(string $name, string $password, string $host = '%')
30
    {
31 104
        $this->name = $name;
32 104
        $this->password = $password;
33 104
        $this->host = $host;
34 104
    }
35
36 71
    public function setPlatform(string $platform): void
37
    {
38 71
        $this->platform = $platform;
39 71
    }
40
41 80
    public function setGrant(string $permission, string $database = '*', string $table = '*'): void
42
    {
43 80
        $this->grants[] = [$permission, $database, $table];
44 80
    }
45
46 8
    public function setGrants(array $permissions, string $database = '*', string $table = '*'): void
47
    {
48 8
        foreach ($permissions as $permission) {
49 8
            $this->setGrant($permission, $database, $table);
50
        }
51 8
    }
52
53 16
    public function toSqlCreate(): string
54
    {
55 16
        $factory = $this->getFactory();
56 16
        $factory->setName($this->name);
57 13
        $factory->setPassword($this->password);
58 13
        $factory->setHost($this->host);
59
60 13
        return $factory->asCreate();
61
    }
62
63 11
    public function toSqlDrop(): string
64
    {
65 11
        $factory = $this->getFactory();
66 11
        $factory->setName($this->name);
67 11
        $factory->setHost($this->host);
68
69 11
        return $factory->asDrop();
70
    }
71
72 80
    public function toSqlPrivileges(): string
73
    {
74 80
        $factory = $this->getFactory();
75 80
        $factory->setName($this->name);
76 80
        $factory->setHost($this->host);
77
78 80
        foreach ($this->grants as $grant) {
79 80
            $factory->setGrant(...$grant);
0 ignored issues
show
Bug introduced by
$grant is expanded, but the parameter $permission of FlexPHP\Database\Interfa...ryInterface::setGrant() does not expect variable arguments. ( Ignorable by Annotation )

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

79
            $factory->setGrant(/** @scrutinizer ignore-type */ ...$grant);
Loading history...
80
        }
81
82 80
        return $factory->asPrivileges();
83
    }
84
85 104
    private function getFactory(): UserFactoryInterface
86
    {
87 104
        $fqdn = \sprintf('FlexPHP\Database\Factories\User\%sUserFactory', $this->platform);
88
89 104
        return new $fqdn();
90
    }
91
}
92