UpdateSectionsCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 34
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A updateWhatRecord() 0 37 4
A configure() 0 7 1
A execute() 0 8 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\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use Symfony\Component\Yaml\Yaml;
21
use Tardigrades\SectionField\Service\SectionManagerInterface;
22
use Tardigrades\SectionField\Service\SectionNotFoundException;
23
use Tardigrades\SectionField\ValueObject\SectionConfig;
24
25
class UpdateSectionsCommand extends SectionCommand
26
{
27
    public function __construct(
28
        SectionManagerInterface $sectionManager
29
    ) {
30
        parent::__construct($sectionManager, 'sf:update-sections');
31
    }
32
33
    protected function configure(): void
34
    {
35
        // @codingStandardsIgnoreStart
36
        $this
37
            ->setDescription('Updates existing sections.')
38
            ->setHelp('This command allows you to update a section based on a yml section configuration. Pass along the path to a section configuration yml. Something like: section/blog.yml')
39
            ->addArgument('config', InputArgument::REQUIRED, 'The section configuration yml')
40
        ;
41
        // @codingStandardsIgnoreEnd
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output): void
45
    {
46
        try {
47
            $sections = $this->sectionManager->readAll();
48
            $this->renderTable($output, $sections, 'All installed Sections');
49
            $this->updateWhatRecord($input, $output);
50
        } catch (SectionNotFoundException $exception) {
51
            $output->writeln("Section not found.");
52
        }
53
    }
54
55
    private function updateWhatRecord(InputInterface $input, OutputInterface $output): void
56
    {
57
        $sections = $this->getSections($input, $output);
58
59
        // because you need to match the section with the selected file, you cannot load multiple sections at once
60
        if (count($sections) > 1) {
61
            $output->writeln('<error>You cannot update multiple sections at once}</error>');
62
            return;
63
        }
64
65
        $config = $input->getArgument('config');
66
67
        foreach ($sections as $section) {
68
            try {
69
                $sectionConfig = SectionConfig::fromArray(
70
                    Yaml::parse(
71
                        file_get_contents($config)
72
                    )
73
                );
74
75
                $inHistory = $this->getHelper('question')->ask(
76
                    $input,
77
                    $output,
78
                    new ConfirmationQuestion(
79
                        '<comment>Do you want to store the current version in history?</comment> (y/n) ',
80
                        false
81
                    )
82
                );
83
84
                $this->sectionManager->updateByConfig($sectionConfig, $section, $inHistory);
85
            } catch (\Exception $exception) {
86
                $output->writeln("<error>Invalid configuration file.  {$exception->getMessage()}</error>");
87
                return;
88
            }
89
90
            $sections = $this->sectionManager->readAll();
91
            $this->renderTable($output, $sections, 'Section updated!');
92
        }
93
    }
94
}
95