Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
4.76% covered (danger)
4.76%
1 / 21
1.23% covered (danger)
1.23%
1 / 81
100.00% covered (success)
100.00%
2 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoadBaseDataCommand
100.00% covered (success)
100.00%
42 / 42
4.76% covered (danger)
4.76%
1 / 21
1.23% covered (danger)
1.23%
1 / 81
100.00% covered (success)
100.00%
2 / 2
29.09
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
41 / 41
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 80
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace App\Command;
4
5use App\Application\UseCase\MaintainWord;
6use App\Dto\Command\MaintainWordCommand;
7use App\Dto\Csv\CsvQualifier;
8use App\Dto\Csv\CsvSubject;
9use App\Enum\Lang;
10use App\Enum\OffenseLevel;
11use App\Enum\QualifierPosition;
12use App\Enum\WordGender;
13use App\Enum\WordStatus;
14use App\Service\Data\WordSluggerInterface;
15use League\Csv\Reader;
16use Symfony\Component\Console\Attribute\AsCommand;
17use Symfony\Component\Console\Attribute\Option;
18use Symfony\Component\Console\Command\Command;
19use Symfony\Component\Console\Output\OutputInterface;
20use Symfony\Component\DependencyInjection\Attribute\Autowire;
21
22/**
23 * @author Wilhelm Zwertvaegher
24 */
25#[AsCommand(name: 'app:load-base-data')]
26class LoadBaseDataCommand extends Command
27{
28    public function __construct(
29        private readonly MaintainWord $maintainWord,
30        private readonly WordSluggerInterface $wordSlugger,
31        #[Autowire('%base_data.subject_csv%')]
32        private readonly string $subjectCsvPath,
33        #[Autowire('%base_data.qualifier_csv%')]
34        private readonly string $qualifierCsvPath,
35    ) {
36        parent::__construct();
37    }
38
39    public function __invoke(
40        OutputInterface $output,
41        #[Option('Append the data fixtures instead of deleting all data from the database first.')] bool $append = false,
42    ): int {
43        $output->writeln('Loading base data...');
44        $output->writeln('Loading subjects...');
45
46        if (!$append) {
47            // TODO : implement data deletion before loading
48        }
49
50        $reader = Reader::from($this->subjectCsvPath);
51        $reader->setHeaderOffset(0);
52        $subjectsSlugs = [];
53
54        foreach ($reader->getRecordsAsObject(CsvSubject::class) as $record) {
55            $fullWordDto = ($this->maintainWord)(
56                new MaintainWordCommand(
57                    label: trim($record->label),
58                    gender: WordGender::fromString($record->gender),
59                    lang: Lang::FR,
60                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
61                    status: WordStatus::APPROVED,
62                    asSubject: true,
63                    handleDeletion: false
64                )
65            );
66            $output->writeln("Created subject {$fullWordDto->label} with word_id {$fullWordDto->id}");
67            $subjectsSlugs[] = $fullWordDto->slug;
68        }
69
70        $output->writeln('Loading qualifiers...');
71
72        $reader = Reader::from($this->qualifierCsvPath);
73        $reader->setHeaderOffset(0);
74
75        foreach ($reader->getRecordsAsObject(CsvQualifier::class) as $record) {
76            $slug = $this->wordSlugger->slug($record->label);
77
78            $fullWordDto = ($this->maintainWord)(
79                new MaintainWordCommand(
80                    label: trim($record->label),
81                    gender: WordGender::fromString($record->gender ?? 'AUTO'),
82                    lang: Lang::FR,
83                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
84                    status: WordStatus::APPROVED,
85                    asSubject: in_array($slug, $subjectsSlugs),
86                    asQualifier: true,
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
88                    handleDeletion: false
89                )
90            );
91            $output->writeln("Created qualifier {$fullWordDto->label} with id {$fullWordDto->id}");
92        }
93
94        $output->writeln('Base data loaded successfully.');
95
96        return Command::SUCCESS;
97    }
98}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

LoadBaseDataCommand->__construct
28    public function __construct(
29        private readonly MaintainWord $maintainWord,
30        private readonly WordSluggerInterface $wordSlugger,
31        #[Autowire('%base_data.subject_csv%')]
32        private readonly string $subjectCsvPath,
33        #[Autowire('%base_data.qualifier_csv%')]
34        private readonly string $qualifierCsvPath,
35    ) {
36        parent::__construct();
37    }
LoadBaseDataCommand->__invoke
39    public function __invoke(
40        OutputInterface $output,
41        #[Option('Append the data fixtures instead of deleting all data from the database first.')] bool $append = false,
42    ): int {
43        $output->writeln('Loading base data...');
44        $output->writeln('Loading subjects...');
45
46        if (!$append) {
50        $reader = Reader::from($this->subjectCsvPath);
51        $reader->setHeaderOffset(0);
52        $subjectsSlugs = [];
53
54        foreach ($reader->getRecordsAsObject(CsvSubject::class) as $record) {
54        foreach ($reader->getRecordsAsObject(CsvSubject::class) as $record) {
55            $fullWordDto = ($this->maintainWord)(
56                new MaintainWordCommand(
57                    label: trim($record->label),
57                    label: trim($record->label),
57                    label: trim($record->label),
54        foreach ($reader->getRecordsAsObject(CsvSubject::class) as $record) {
55            $fullWordDto = ($this->maintainWord)(
56                new MaintainWordCommand(
57                    label: trim($record->label),
54        foreach ($reader->getRecordsAsObject(CsvSubject::class) as $record) {
55            $fullWordDto = ($this->maintainWord)(
56                new MaintainWordCommand(
57                    label: trim($record->label),
58                    gender: WordGender::fromString($record->gender),
59                    lang: Lang::FR,
60                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
61                    status: WordStatus::APPROVED,
62                    asSubject: true,
63                    handleDeletion: false
64                )
65            );
66            $output->writeln("Created subject {$fullWordDto->label} with word_id {$fullWordDto->id}");
67            $subjectsSlugs[] = $fullWordDto->slug;
68        }
69
70        $output->writeln('Loading qualifiers...');
71
72        $reader = Reader::from($this->qualifierCsvPath);
73        $reader->setHeaderOffset(0);
74
75        foreach ($reader->getRecordsAsObject(CsvQualifier::class) as $record) {
75        foreach ($reader->getRecordsAsObject(CsvQualifier::class) as $record) {
76            $slug = $this->wordSlugger->slug($record->label);
77
78            $fullWordDto = ($this->maintainWord)(
79                new MaintainWordCommand(
80                    label: trim($record->label),
80                    label: trim($record->label),
80                    label: trim($record->label),
80                    label: trim($record->label),
81                    gender: WordGender::fromString($record->gender ?? 'AUTO'),
82                    lang: Lang::FR,
83                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
84                    status: WordStatus::APPROVED,
85                    asSubject: in_array($slug, $subjectsSlugs),
85                    asSubject: in_array($slug, $subjectsSlugs),
85                    asSubject: in_array($slug, $subjectsSlugs),
85                    asSubject: in_array($slug, $subjectsSlugs),
86                    asQualifier: true,
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
75        foreach ($reader->getRecordsAsObject(CsvQualifier::class) as $record) {
76            $slug = $this->wordSlugger->slug($record->label);
77
78            $fullWordDto = ($this->maintainWord)(
79                new MaintainWordCommand(
80                    label: trim($record->label),
81                    gender: WordGender::fromString($record->gender ?? 'AUTO'),
82                    lang: Lang::FR,
83                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
84                    status: WordStatus::APPROVED,
85                    asSubject: in_array($slug, $subjectsSlugs),
86                    asQualifier: true,
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
75        foreach ($reader->getRecordsAsObject(CsvQualifier::class) as $record) {
76            $slug = $this->wordSlugger->slug($record->label);
77
78            $fullWordDto = ($this->maintainWord)(
79                new MaintainWordCommand(
80                    label: trim($record->label),
81                    gender: WordGender::fromString($record->gender ?? 'AUTO'),
82                    lang: Lang::FR,
83                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
84                    status: WordStatus::APPROVED,
85                    asSubject: in_array($slug, $subjectsSlugs),
86                    asQualifier: true,
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
88                    handleDeletion: false
89                )
90            );
91            $output->writeln("Created qualifier {$fullWordDto->label} with id {$fullWordDto->id}");
92        }
93
94        $output->writeln('Base data loaded successfully.');
95
96        return Command::SUCCESS;
97    }
{main}
3namespace App\Command;
4
5use App\Application\UseCase\MaintainWord;
6use App\Dto\Command\MaintainWordCommand;
7use App\Dto\Csv\CsvQualifier;
8use App\Dto\Csv\CsvSubject;
9use App\Enum\Lang;
10use App\Enum\OffenseLevel;
11use App\Enum\QualifierPosition;
12use App\Enum\WordGender;
13use App\Enum\WordStatus;
14use App\Service\Data\WordSluggerInterface;
15use League\Csv\Reader;
16use Symfony\Component\Console\Attribute\AsCommand;
17use Symfony\Component\Console\Attribute\Option;
18use Symfony\Component\Console\Command\Command;
19use Symfony\Component\Console\Output\OutputInterface;
20use Symfony\Component\DependencyInjection\Attribute\Autowire;
21
22/**
23 * @author Wilhelm Zwertvaegher
24 */
25#[AsCommand(name: 'app:load-base-data')]
26class LoadBaseDataCommand extends Command
27{
28    public function __construct(
29        private readonly MaintainWord $maintainWord,
30        private readonly WordSluggerInterface $wordSlugger,
31        #[Autowire('%base_data.subject_csv%')]
32        private readonly string $subjectCsvPath,
33        #[Autowire('%base_data.qualifier_csv%')]
34        private readonly string $qualifierCsvPath,
35    ) {
36        parent::__construct();
37    }
38
39    public function __invoke(
40        OutputInterface $output,
41        #[Option('Append the data fixtures instead of deleting all data from the database first.')] bool $append = false,
42    ): int {
43        $output->writeln('Loading base data...');
44        $output->writeln('Loading subjects...');
45
46        if (!$append) {
47            // TODO : implement data deletion before loading
48        }
49
50        $reader = Reader::from($this->subjectCsvPath);
51        $reader->setHeaderOffset(0);
52        $subjectsSlugs = [];
53
54        foreach ($reader->getRecordsAsObject(CsvSubject::class) as $record) {
55            $fullWordDto = ($this->maintainWord)(
56                new MaintainWordCommand(
57                    label: trim($record->label),
58                    gender: WordGender::fromString($record->gender),
59                    lang: Lang::FR,
60                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
61                    status: WordStatus::APPROVED,
62                    asSubject: true,
63                    handleDeletion: false
64                )
65            );
66            $output->writeln("Created subject {$fullWordDto->label} with word_id {$fullWordDto->id}");
67            $subjectsSlugs[] = $fullWordDto->slug;
68        }
69
70        $output->writeln('Loading qualifiers...');
71
72        $reader = Reader::from($this->qualifierCsvPath);
73        $reader->setHeaderOffset(0);
74
75        foreach ($reader->getRecordsAsObject(CsvQualifier::class) as $record) {
76            $slug = $this->wordSlugger->slug($record->label);
77
78            $fullWordDto = ($this->maintainWord)(
79                new MaintainWordCommand(
80                    label: trim($record->label),
81                    gender: WordGender::fromString($record->gender ?? 'AUTO'),
82                    lang: Lang::FR,
83                    offenseLevel: OffenseLevel::fromString($record->offenseLevel ?? 'MEDIUM'),
84                    status: WordStatus::APPROVED,
85                    asSubject: in_array($slug, $subjectsSlugs),
86                    asQualifier: true,
87                    qualifierPosition: QualifierPosition::from(trim($record->position)) ?? 'after',
88                    handleDeletion: false
89                )
90            );
91            $output->writeln("Created qualifier {$fullWordDto->label} with id {$fullWordDto->id}");
92        }
93
94        $output->writeln('Base data loaded successfully.');
95
96        return Command::SUCCESS;
97    }