Issues (34)

src/Command/UpdateApplicationCommand.php (1 issue)

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\Helper\QuestionHelper;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Question\Question;
21
use Symfony\Component\Yaml\Yaml;
22
use Tardigrades\Entity\ApplicationInterface;
23
use Tardigrades\SectionField\Service\ApplicationManagerInterface;
24
use Tardigrades\SectionField\Service\ApplicationNotFoundException;
25
use Tardigrades\SectionField\ValueObject\ApplicationConfig;
26
use Tardigrades\SectionField\ValueObject\Id;
27
28
class UpdateApplicationCommand extends ApplicationCommand
29
{
30
    /** @var QuestionHelper */
31
    private $questionHelper;
32
33
    /** @var ApplicationManagerInterface */
34
    private $applicationManager;
35
36
    public function __construct(
37
        ApplicationManagerInterface $applicationManager
38
    ) {
39
        $this->applicationManager = $applicationManager;
40
41
        parent::__construct('sf:update-application');
42
    }
43
44
    protected function configure(): void
45
    {
46
        // @codingStandardsIgnoreStart
47
        $this
48
            ->setDescription('Updates an existing application.')
49
            ->setHelp('This command allows you to update an application based on a yml application configuration. Pass along the path to a application configuration yml. Something like: application/application.yml')
50
            ->addArgument('config', InputArgument::REQUIRED, 'The application configuration yml')
51
        ;
52
        // @codingStandardsIgnoreEnd
53
    }
54
55
    protected function execute(InputInterface $input, OutputInterface $output): void
56
    {
57
        try {
58
            $this->questionHelper = $this->getHelper('question');
59
            $this->showInstalledApplications($input, $output);
60
        } catch (ApplicationNotFoundException $exception) {
61
            $output->writeln("Section not found");
62
        }
63
    }
64
65
    private function showInstalledApplications(InputInterface $input, OutputInterface $output): void
66
    {
67
        $applications = $this->applicationManager->readAll();
68
69
        $this->renderTable($output, $applications, 'All installed Applications');
70
        $this->updateWhatRecord($input, $output);
71
    }
72
73
    private function getApplicationRecord(InputInterface $input, OutputInterface $output): ApplicationInterface
74
    {
75
        $question = new Question('<question>What record do you want to update?</question> (#id): ');
76
        $question->setValidator(function ($id) {
77
            try {
78
                return $this->applicationManager->read(Id::fromInt((int) $id));
79
            } catch (ApplicationNotFoundException $exception) {
80
                return null;
81
            }
82
        });
83
84
        $applicationRecord = $this->questionHelper->ask($input, $output, $question);
85
        if (!$applicationRecord) {
86
            throw new ApplicationNotFoundException();
87
        }
88
        return $applicationRecord;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $applicationRecord could return the type string|true which is incompatible with the type-hinted return Tardigrades\Entity\ApplicationInterface. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91
    private function updateWhatRecord(InputInterface $input, OutputInterface $output): void
92
    {
93
        $application = $this->getApplicationRecord($input, $output);
94
        $config = $input->getArgument('config');
95
96
        try {
97
            $applicationConfig = ApplicationConfig::fromArray(
98
                Yaml::parse(
99
                    file_get_contents($config)
100
                )
101
            );
102
            $this->applicationManager->updateByConfig($applicationConfig, $application);
103
        } catch (\Exception $exception) {
104
            $output->writeln("<error>Invalid configuration file.  {$exception->getMessage()}</error>");
105
            return;
106
        }
107
108
        $output->writeln('<info>Application updated!</info>');
109
    }
110
}
111