Lines 80.00% 28 / 35
Functions and Methods 66.66% 4 / 6
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ProjectCheck 80.00% 28 / 35 66.66% 4 / 6 16.80 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 checkProject 70.58% 12 / 17 0.00% 0 / 1 5.64
 findHttpListenerError 100.00% 4 / 4 100.00% 1 / 1 2
 findAppBuildFailures 100.00% 5 / 5 100.00% 1 / 1 3
 listBuildErrorsInAppOrder 100.00% 6 / 6 100.00% 1 / 1 3
 describeUnloadableFile 0.00% 0 / 2 0.00% 0 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Console\Check;
6
7use Stewart\Contracts\Exception\StewartException;
8use Stewart\Runtime\App\AppCatalog;
9use Stewart\Runtime\App\AppCatalogResolver;
10use Stewart\Runtime\App\AppDiscovery;
11use Stewart\Runtime\App\AppInstanceResolver;
12use Stewart\Runtime\App\AppSelection;
13use Stewart\Runtime\App\GeneratedRoots;
14use Stewart\Runtime\App\UnloadableAppFile;
15use Stewart\Runtime\Broker\AppPlacement;
16use Stewart\Runtime\Broker\Collection\WorkerSlotCollection;
17use Stewart\Runtime\Config\ConfigLoader;
18use Stewart\Runtime\Config\HttpConfig;
19use Stewart\Runtime\Container\AppContainerBlueprint;
20use Stewart\Runtime\Container\AppContainerBuilder;
21use Stewart\Runtime\Container\Collection\AppBuildFailureCollection;
22
23final readonly class ProjectCheck
24{
25    public function __construct(
26        private ConfigLoader $config,
27        private AppDiscovery $discovery,
28        private AppInstanceResolver $instances,
29        private AppCatalogResolver $catalogs,
30        private AppPlacement $placement,
31        private AppContainerBuilder $containers,
32        private string $userServicesFile,
33    ) {}
34
35    // Errors come in the order the Git deploy gate reports them: its first line is the rejection reason.
36    public function checkProject(?string $configFile, AppSelection $selection = new AppSelection()): ProjectCheckResult
37    {
38        try {
39            $config = $this->config->loadConfig($configFile);
40            $config->requireHomeAssistant();
41            $generated = GeneratedRoots::fromNamespace($config->codegen->namespace->value);
42            $discovered = $this->instances->resolveInstances($this->discovery->discover(), $config->apps);
43            $catalog = $this->catalogs->resolveCatalog($discovered, $config->apps, $config->workers, $selection);
44        } catch (StewartException $e) {
45            return ProjectCheckResult::failed([$e->getMessage()]);
46        }
47
48        if (!$discovered->unloadable->isEmpty()) {
49            return ProjectCheckResult::failed($discovered->unloadable->mapToList($this->describeUnloadableFile(...)));
50        }
51
52        $workerSlots = $this->placement->planWorkerSlots($catalog->enabled, $config);
53
54        try {
55            $failures = $this->findAppBuildFailures($workerSlots, AppContainerBlueprint::fromProjectConfig($config, $generated, $catalog->knownIds, $this->userServicesFile));
56        } catch (StewartException $e) {
57            return ProjectCheckResult::failed([$e->getMessage()]);
58        }
59
60        $errors = $this->listBuildErrorsInAppOrder($catalog, $failures);
61
62        return $errors === []
63            ? ProjectCheckResult::passed($discovered, $catalog, $workerSlots, $config->codegen, $this->findHttpListenerError($config->http))
64            : ProjectCheckResult::failed($errors);
65    }
66
67    // Only a notice for check: CI checks a project without its secrets, which the daemon gets at deploy time.
68    private function findHttpListenerError(HttpConfig $http): ?string
69    {
70        try {
71            $http->requireValidListeners();
72        } catch (StewartException $e) {
73            return $e->getMessage();
74        }
75
76        return null;
77    }
78
79    /** @throws StewartException */
80    private function findAppBuildFailures(WorkerSlotCollection $workerSlots, AppContainerBlueprint $blueprint): AppBuildFailureCollection
81    {
82        $failures = [];
83
84        foreach ($workerSlots as $slot) {
85            foreach ($this->containers->findAppBuildFailures($slot->listWorkerApps(), $blueprint, $slot->workerId) as $failure) {
86                $failures[] = $failure;
87            }
88        }
89
90        return AppBuildFailureCollection::keyedByAppId($failures);
91    }
92
93    /** @return list<string> */
94    private function listBuildErrorsInAppOrder(AppCatalog $catalog, AppBuildFailureCollection $failures): array
95    {
96        $errors = [];
97
98        foreach ($catalog->enabled as $app) {
99            $failure = $failures->find($app->id);
100
101            if ($failure !== null) {
102                $errors[] = $failure->error->getMessage();
103            }
104        }
105
106        return $errors;
107    }
108
109    private function describeUnloadableFile(UnloadableAppFile $file): string
110    {
111        $cause = str_replace(["\r", "\n"], ' ', $file->cause->getMessage());
112
113        return \sprintf('%s failed to load: %s', $file->path, $cause);
114    }
115}