RestoreSectionCommand::restoreWhatRecord()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 28
rs 9.7333
cc 4
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\Command;
15
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
use Symfony\Component\Console\Question\Question;
20
use Tardigrades\Entity\SectionInterface;
21
use Tardigrades\SectionField\Service\SectionHistoryManagerInterface;
22
use Tardigrades\SectionField\Service\SectionHistoryNotFoundException;
23
use Tardigrades\SectionField\Service\SectionManagerInterface;
24
use Tardigrades\SectionField\Service\SectionNotFoundException;
25
use Tardigrades\SectionField\ValueObject\Id;
26
27
class RestoreSectionCommand extends SectionCommand
28
{
29
    /** @var SectionHistoryManagerInterface */
30
    private $sectionHistoryManager;
31
32
    public function __construct(
33
        SectionManagerInterface $sectionManager,
34
        SectionHistoryManagerInterface $sectionHistoryManager
35
    ) {
36
        $this->sectionHistoryManager = $sectionHistoryManager;
37
38
        parent::__construct($sectionManager, 'sf:restore-section');
39
    }
40
41
    protected function configure(): void
42
    {
43
        $this
44
            ->setDescription('Restore a section from history.')
45
            ->setHelp('Choose a section from history to move back to the active section position')
46
        ;
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output): void
50
    {
51
        try {
52
            $sections = $this->sectionManager->readAll();
53
            $this->renderTable($output, $sections, 'All installed Sections');
54
            $this->restoreWhatRecord($input, $output);
55
        } catch (SectionNotFoundException $exception) {
56
            $output->writeln('Section not found.');
57
        } catch (SectionHistoryNotFoundException $exception) {
58
            $output->writeln('Section history not found.');
59
        }
60
    }
61
62
    private function restoreWhatRecord(InputInterface $input, OutputInterface $output): void
63
    {
64
        $sections = $this->getSections($input, $output);
65
66
        foreach ($sections as $section) {
67
            $output->writeln('<info>Record with id #' . $section->getId() .
68
                ' will be restored, select a record from history to restore the section with.</info>');
69
70
            $history = $section->getHistory()->toArray();
71
72
            // no need to show a restore option, if there is nothing to restore
73
            if (count($history) == 0) {
74
                $output->writeln('<comment>Skipped, no records can be found in history...</comment>');
75
                continue;
76
            }
77
78
            $this->renderTable($output, $history, 'Section history');
79
            $sectionFromHistory = $this->getSectionFromHistory($input, $output);
80
81
            $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false);
82
            if (!$this->getHelper('question')->ask($input, $output, $sure)) {
83
                $output->writeln('<comment>Cancelled, nothing restored.</comment>');
84
                return;
85
            }
86
87
            $this->sectionManager->restoreFromHistory($sectionFromHistory);
88
89
            $output->writeln('<info>Config Restored! Run the generate-section command to finish rollback.</info>');
90
        }
91
    }
92
93
    protected function getSectionFromHistory(InputInterface $input, OutputInterface $output): SectionInterface
94
    {
95
        $question = new Question('<question>Choose record.</question> (#id): ');
96
97
        $question->setValidator(function ($id) {
98
            try {
99
                return $this->sectionHistoryManager->read(Id::fromInt((int) $id));
100
            } catch (SectionHistoryNotFoundException $exception) {
101
                return null;
102
            }
103
        });
104
105
        $sectionFromHistory = $this->getHelper('question')->ask($input, $output, $question);
106
        if (!$sectionFromHistory) {
107
            throw new SectionHistoryNotFoundException();
108
        }
109
        return $sectionFromHistory;
110
    }
111
}
112