Test Failed
Push — master ( ac767f...c4c534 )
by Sergei
03:25
created

SchemaClearCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 59
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 52
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 59
rs 9.0472

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Command\Fixture;
6
7
use App\Blog\Entity\PostTag;
8
use App\Blog\Entity\Tag;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Yiisoft\Yii\Console\ExitCode;
13
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
14
use App\Blog\Entity\Comment;
15
use App\Blog\Entity\Post;
16
use App\User\User;
17
18
final class SchemaClearCommand extends Command
19
{
20
    protected static $defaultName = 'fixture/schema/clear';
21
22
    private CycleDependencyProxy $promise;
23
24
    public function __construct(
25
        CycleDependencyProxy $promise,
26
    ) {
27
        $this->promise = $promise;
28
        parent::__construct();
29
    }
30
31
    public function configure(): void
32
    {
33
        $this
34
            ->setDescription('Clear database from fixtures')
35
            ->setHelp('This command delete all tables');
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40
        $this->promise
41
            ->getDatabaseProvider()
42
            ->database()
43
            ->delete('post')
44
            ->run();
45
46
        $this->promise
47
            ->getDatabaseProvider()
48
            ->database()
49
            ->delete('post_tag')
50
            ->run();
51
52
        $this->promise
53
            ->getDatabaseProvider()
54
            ->database()
55
            ->delete('tag')
56
            ->run();
57
58
        $this->promise
59
            ->getDatabaseProvider()
60
            ->database()
61
            ->delete('user')
62
            ->run();
63
64
        $this->promise
65
            ->getDatabaseProvider()
66
            ->database()
67
            ->delete('comment')
68
            ->run();
69
70
        return 0 === $this->promise
71
                ->getORM()
72
                ->getRepository(Post::class)
73
                ->select()
74
                ->count() +
75
            $this->promise
76
                ->getORM()
77
                ->getRepository(PostTag::class)
78
                ->select()
79
                ->count() +
80
            $this->promise
81
                ->getORM()
82
                ->getRepository(Tag::class)
83
                ->select()
84
                ->count() +
85
            $this->promise
86
                ->getORM()
87
                ->getRepository(User::class)
88
                ->select()
89
                ->count() +
90
            $this->promise
91
                ->getORM()
92
                ->getRepository(Comment::class)
93
                ->select()
94
                ->count()
95
            ? ExitCode::OK
96
            : ExitCode::UNSPECIFIED_ERROR;
97
    }
98
}
99