flexphp /
flex-database
| 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
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 |