Passed
Push — master ( 77290b...f3fb78 )
by Marcel
09:09
created

SetupCommand::setupRememberMe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 2
rs 9.9332
1
<?php
2
3
namespace App\Command;
4
5
use App\Setup\UserTypesSetup;
6
use Doctrine\DBAL\Connection;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
13
14
#[AsCommand(name: 'app:setup', description: 'Runs the initial setup.')]
15
class SetupCommand extends Command {
16
17
    public function __construct(private readonly Connection $dbalConnection, private readonly UserTypesSetup $userTypeSetup, private readonly PdoSessionHandler $pdoSessionHandler, ?string $name = null) {
18
        parent::__construct($name);
19
    }
20
21
    public function execute(InputInterface $input, OutputInterface $output): int {
22
        $io = new SymfonyStyle($input, $output);
23
24
        $output->write('Add default user type...');
25
        $this->addDefaultUserType();
26
        $output->writeln('<fg=green>OK</>');
27
28
        $output->write('Create sessions table...');
29
        $this->setupSessions();
30
        $output->writeln('<fg=green>OK</>');
31
32
        $output->write('Create remember me table...');
33
        $this->setupRememberMe();
34
        $output->writeln('<fg=green>OK</>');
35
36
        $io->success('Setup completed');
37
38
        return 0;
39
    }
40
41
    private function addDefaultUserType() {
42
        $this->userTypeSetup->setupDefaultUserTypes();
43
    }
44
45
    private function setupRememberMe() {
46
        $sql = <<<SQL
47
CREATE TABLE IF NOT EXISTS `rememberme_token` (
48
    `series`   char(88)     UNIQUE PRIMARY KEY NOT NULL,
49
    `value`    char(88)     NOT NULL,
50
    `lastUsed` datetime     NOT NULL,
51
    `class`    varchar(100) NOT NULL,
52
    `username` varchar(200) NOT NULL
53
);
54
SQL;
55
56
        $this->dbalConnection->executeQuery($sql);
57
    }
58
59
    private function setupSessions() {
60
        $sql = "SHOW TABLES LIKE 'sessions';";
61
        $row = $this->dbalConnection->executeQuery($sql);
62
63
        if($row->fetchAssociative() === false) {
64
            $this->pdoSessionHandler->createTable();
65
        }
66
    }
67
}