• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / bind-activerecord / 6362244861

30 Sep 2023 10:37AM UTC coverage: 87.931% (+24.7%) from 63.265%
6362244861

push

github

olvlvl
Rename ConfigBuilder::add_model as add_record

2 of 2 new or added lines in 1 file covered. (100.0%)

102 of 116 relevant lines covered (87.93%)

1.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.63
/lib/Console/InstallCommand.php
1
<?php
2

3
namespace ICanBoogie\Binding\ActiveRecord\Console;
4

5
use ICanBoogie\ActiveRecord;
6
use ICanBoogie\ActiveRecord\ModelIterator;
7
use ICanBoogie\ActiveRecord\ModelProvider;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Throwable;
14

15
#[AsCommand('activerecord:install', "Install models")]
16
final class InstallCommand extends Command
17
{
18
    public function __construct(
19
        private readonly ModelProvider $models,
20
        private readonly ModelIterator $iterator,
21
        private readonly string $style,
22
    ) {
23
        parent::__construct();
1✔
24
    }
25

26
    protected function execute(InputInterface $input, OutputInterface $output): int
27
    {
28
        /** @var array<class-string<ActiveRecord>, bool> $tried */
29
        $tried = [];
1✔
30
        /** @var array<class-string<ActiveRecord>, true> $installed */
31
        $installed = [];
1✔
32
        /** @var array<class-string<ActiveRecord>, true> $already_installed */
33
        $already_installed = [];
1✔
34
        /** @var array<class-string<ActiveRecord>, string> $errors */
35
        $errors = [];
1✔
36

37
        $recursive_install = function (string $activerecord_class) use (
1✔
38
            &$recursive_install,
1✔
39
            &$tried,
1✔
40
            &$installed,
1✔
41
            &$already_installed,
1✔
42
            &$errors,
1✔
43
        ): bool {
1✔
44
            /** @var class-string<ActiveRecord> $activerecord_class */
45

46
            if (isset($tried[$activerecord_class])) {
1✔
47
                return $tried[$activerecord_class];
1✔
48
            }
49

50
            $model = $this->models->model_for_record($activerecord_class);
1✔
51

52
            if ($model->is_installed()) {
1✔
53
                $already_installed[$activerecord_class] = true;
×
54

55
                return $tried[$activerecord_class] = true;
×
56
            }
57

58
            $parent_activerecord_class = $model->parent?->activerecord_class;
1✔
59

60
            if ($parent_activerecord_class) {
1✔
61
                $rc = $recursive_install($parent_activerecord_class);
1✔
62

63
                if (!$rc) {
1✔
64
                    $errors[$activerecord_class] = "Parent install failed: '$parent_activerecord_class'";
×
65

66
                    return $tried[$activerecord_class] = false;
×
67
                }
68
            }
69

70
            try {
71
                $model->install();
1✔
72
                $installed[$activerecord_class] = true;
1✔
73

74
                return $tried[$activerecord_class] = true;
1✔
75
            } catch (Throwable $e) {
×
76
                $errors[$activerecord_class] = $e->getMessage();
×
77
            }
78

79
            return $tried[$activerecord_class] = false;
×
80
        };
1✔
81

82
        $rows = [];
1✔
83

84
        foreach ($this->iterator->model_iterator() as $activerecord_class => $_) {
1✔
85
            $recursive_install($activerecord_class);
1✔
86

87
            $rows[] = [
1✔
88
                $activerecord_class,
1✔
89
                isset($already_installed[$activerecord_class])
1✔
90
                    ? "Already"
×
91
                    : (isset($installed[$activerecord_class]) ? "Yes" : "No"),
1✔
92
                $errors[$activerecord_class] ?? "",
1✔
93
            ];
1✔
94
        }
95

96
        $table = new Table($output);
1✔
97
        $table->setHeaders([ 'Record', 'Installed', 'Error' ]);
1✔
98
        $table->setRows($rows);
1✔
99
        $table->setStyle($this->style);
1✔
100
        $table->render();
1✔
101

102
        return count($errors)
1✔
103
            ? Command::FAILURE
×
104
            : Command::SUCCESS;
1✔
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc